将 Bool 值更改为 Int

Change Bool value to Int

我正在寻求您的帮助来制作我的第一个应用程序。我白天是盲人和视障人士的老师,我正在尝试制作一个应用程序用于我们对学生的评估。在这个特定的部分,我们正在评估他们的 Positional/Spatial 技能。我们喜欢使用切换的便利性,但我很难将 bool 值更改为 Int 以获得每个部分的分数。我想要 true = 1 和 false = 0。数字分数将在每个部分下方列出,我已经将其编码出来。

我曾尝试使用“Extension”来更改值,但一直出现错误。我可以将它们全部更改为分段选择器并接近相同的易用性,但我希望有一个更简单的修复程序,因为我已经编写了大约 100 个这样的代码。提前致谢!

struct caneSkills: View {
    @State var front = false
    @State var behind = false
    @State var onTop = false
    @State var below = false
    @State var between = false
    @State var nextTo = false
    @State var under = false
    @State var onBottom = false
    @State var inBack = false
    @State var onTheSide = false
    var body: some View {
        let psScore = (front + behind + onTop + below + between + nextTo + under + onBottom + inBack + onTheSide)
        NavigationView {
            Form {
                Section(header: Text("Positional/Spatial Concepts")) {
                    Toggle("Behind", isOn: $behind)
                    Toggle("Below", isOn: $below)
                    Toggle("In Back", isOn: $inBack)
                    Toggle("In Between", isOn: $between)
                    Toggle("In Front", isOn: $front)
                    Toggle("Next To", isOn: $nextTo)
                    Toggle("On The Bottom", isOn: $onBottom)
                    Toggle("On Side", isOn: $onTheSide)
                    Toggle("On Top", isOn: $onTop)
                    Group {
                        Toggle("Under", isOn: $under)
                        Text("Positional/Spatial Concepts Score: \(psScore) / 10")
                            .font(.headline)
                        .foregroundColor(Color.blue)
                    }
                }
            }
            .navigationTitle("Cane Skills")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }       
}

我认为您不需要将 Bool 转换为 Int 来获得总分。您可以使用这种简单的方法获得分数:

struct caneSkills: View {
    @State var front = false
    @State var behind = false
    @State var onTop = false
    @State var below = false
    @State var between = false
    @State var nextTo = false
    @State var under = false
    @State var onBottom = false
    @State var inBack = false
    @State var onTheSide = false
    
    // -- here count all true values
    var psScore: Int {
       [front,behind,onTop,below,between,nextTo,under,onBottom,inBack,onTheSide].filter{ [=10=] }.count
    }

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Positional/Spatial Concepts")) {
                    Toggle("Behind", isOn:$behind)
                    Toggle("Below", isOn:$below)
                    Toggle("In Back", isOn:$inBack)
                    Toggle("In Between", isOn:$between)
                    Toggle("In Front", isOn:$front)
                    Toggle("Next To", isOn:$nextTo)
                    Toggle("On The Bottom", isOn:$onBottom)
                    Toggle("On Side", isOn:$onTheSide)
                    Toggle("On Top", isOn:$onTop)
                    Group{
                        Toggle("Under", isOn:$under)
                        Text("Positional/Spatial Concepts Score: \(psScore) / 10")
                            .font(.headline)
                            .foregroundColor(Color.blue)
                    }
                }
            }
            .navigationTitle("Cane Skills")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

为了好玩,您可以扩展 Bool 类型并向其添加值 属性。然后您可以简单地创建一个自定义运算符来对所有 bool 元素求和:

extension Bool {
    var value: Int { self ? 1 : 0 }
    public static func + (lhs: Int, rhs: Bool) -> Int {
        lhs + rhs.value
    }
}

我也会构建数据 Skill。这样你就可以拥有一个包含所有技能的数组:

struct Skill {
    let name: String
    var isOn: Bool
}

struct CaneSkills: View {
    
    @State var skills = [
        ("Behind", false),
        ("Below", false),
        ("In Back", false),
        ("In Between", false),
        ("In Front", false),
        ("Next To", false),
        ("On The Bottom", false),
        ("On Side", false),
        ("On Top", false),
        ("Under", false)
    ].map(Skill.init)
    
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Positional/Spatial Concepts")) {
                    ForEach(skills.indices, id: \.self) { index in
                        Toggle(skills[index].name, isOn: $skills[index].isOn)
                    }
                    Text("Score: \(psScore) / 10")
                        .font(.headline)
                        .monospacedDigit()
                        .foregroundColor(Color.blue)
                        .frame(maxWidth: .infinity, alignment: .trailing)                    }
            }
            .navigationTitle("Cane Skills")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

extension CaneSkills {
    var psScore: Int {
        skills.reduce(0) { [=13=] + .isOn }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        CaneSkills()
    }
}