【问题标题】:Please help: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols请帮忙:类型'()'不能符合'View';只有结构/枚举/类类型可以符合协议
【发布时间】:2021-02-08 06:07:59
【问题描述】:

我正在尝试编写包含 If 语句的代码,该语句取决于用户输入的一系列问题的响应。不同的响应将为变量“Score”添加不同的值。我的代码相当简单,但我不明白为什么错误消息“类型'()'不能符合'视图';只有结构/枚举/类类型可以符合协议”在我的 If 语句周围不断弹出。 If 语句位于最后。感谢任何愿意提供帮助的人!

import SwiftUI

struct InfluenceLevel: View {
    @State private var name = ""
    @State private var age = "0"
    @State private var gender = ""
    @State private var nationality = ""
    @State private var Instagram = ""
    @State private var Celebrities = ""
    @State private var Time = ""
    @State private var products = ""
    @State private var score = 0

    
    var body: some View {
        VStack {
            Text("Please fill out the following questions to the best of your ability, allowing us to assess your susceptibility to advertising on social media")

            Form{
                Group{
                    
                    
                    
                    Text("What is your gender? \(gender)" )
                    TextField("(Male/Female/Other):", text: $gender)
                    
                    
                    
                    Text("Are you Chinese? \(nationality)" )
                    TextField("(Yes/No):", text: $nationality)
                    
                    
                    Text("Do you use Instagram? \(Instagram)" )
                    TextField("(Yes/No):", text: $Instagram)
                }
                Group{
                    
                    Text("Do you mainly follow celebrities on social media? \(Celebrities)" )
                    TextField("(Yes/No):", text: $Celebrities)
                    
                
                    Text("Do you spend less than an hour on social media every day? \(Time)" )
                    TextField("(Yes/No)", text: $Time)
                    
                    Text("Do you use more than half of your monthly expenditure on stuff seen online? \(products)" )
                    TextField("(Yes/No)", text: $products)
                    
                    
                
                
                }
            }
        }
            .navigationBarTitle("Level of Influence")
        if gender == "Male" {
            score = score + 1
        }
    }
}

struct InfluenceLevel_Previews: PreviewProvider {
    static var previews: some View {
        InfluenceLevel()
    }
}

【问题讨论】:

  • 您应该使用枚举来表示受约束的值,例如 Gender 和 Nationality,因为现在如果有人键入 "male" 而不是 "Male"(如 @987654325 @ 区分大小写)。
  • 另外,我很好奇为什么你们的节目认为男性比女性更有影响力...
  • @戴。我对快速编程很陌生。你能告诉我我会怎么做吗?非常感谢
  • @戴。哈哈,这是基于我对我们学校的一群学生所做的一项小型调查。我根据从调查中收集的数据得出这个测验的结果。这不是个人偏见。此外,影响程度不是性别的影响程度,而是社交媒体对每个性别的影响程度。这些问题旨在帮助人们根据我们收集的数据了解他们对在社交媒体上投放广告的敏感程度。

标签: swift


【解决方案1】:

试试这个:

enum GenderEnum: Int {
    case female, male, other
    
    init(str: String) {
        switch str {
        case "Male":
            self = .male
        case "Female":
            self = .female
        default:
            self = .other
        }
    }
}

struct InfluenceLevel: View {
    @State private var gender = ""
    @State private var score = 0

    
    var body: some View {
                
        let binding = Binding<String>(get: {
                    self.gender
                }, set: {
                    self.gender = $0
                    score = GenderEnum(str: $0).rawValue
                    print(score)
                })
        
        VStack {
            Text("Please fill out the following questions to the best of your ability, allowing us to assess your susceptibility to advertising on social media")

            Form{
                Group{
                    
                    Text("What is your gender? \(gender)" )
                    TextField("(Male/Female/Other):", text: binding)
            }
        }
        .navigationBarTitle("Level of Influence")
    }
}

【讨论】:

  • 谢谢你,玛雅尼。但是我如何才能显示该人的最终分数,以及如何针对每个问题(即国籍、年龄等)做到这一点。
  • 这只是一个变量的例子,可以多次使用,也可以全部相乘。
  • 你介意解释一下我如何根据他们的答案来加分吗?我对 Swift 很陌生,所以我发现很难理解你的代码是如何工作的。谢谢!
猜你喜欢
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多