请帮助:类型“()”不能符合“视图”;只有结构/枚举/类类型可以符合协议

时间:2021-02-08 06:07:59

标签: swift

我正在尝试编写包含 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()
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

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")
    }
}
相关问题