将Bool值更改为INT

发布于 2025-01-22 23:54:17 字数 1810 浏览 0 评论 0原文

我呼吁您寻求制作第一个应用程序的帮助。我是白天盲人和视力障碍的老师,正在尝试将应用程序用于我们的学生评估。在此特定部分中,我们正在评估他们的位置/空间技能。我们喜欢使用切换的便捷性,但是我很难将BOOL值更改为INT,以便获得每个部分的分数。我想要true = 1和false = 0。数值得分将在每个部分下方列出,我已经对其进行了编码。

我试图使用“扩展”来更改值,但要继续遇到错误。我可以将它们全部更改为一个分段的选择器,并接近相同的易用性,但是希望有一个更容易的修复程序,因为我已经编码了其中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())
    }       
}

I am calling for your help on my quest of making my first app. I am a teacher for the blind and visually impaired by day and am trying to make an app to use for our evaluations of students. In this particular section, we are evaluating their Positional/Spatial skills. We like the ease of using a toggle but I am having a hard time changing the bool value to an Int in order to get a score for each section. I would like true = 1 and false = 0. The numerical score will be listed below each section and I already have that coded out.

I have tried to use "Extension" to change the value but keep getting an error. I could change them all to a segmented picker and get close to the same ease of use but was hoping there was an easier fix since I've already coded about 100 of these. Thanks in advance!

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())
    }       
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2025-01-29 23:54:18

只是为了娱乐,您可以扩展Bool类型并向其添加值属性。然后,您可以简单地创建一个自定义操作员来总结所有BOOL元素:

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

我还将构建数据squill。这样,您就可以拥有一个具有所有技能的数组:

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) { $0 + $1.isOn }
    }
}

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

Just for fun you can extend Bool type and add a value property to it. Then you can simply create a custom operator to sum all bool elements:

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

I would also structure the data Skill. This way you can have an array with all your skills:

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) { $0 + $1.isOn }
    }
}

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

enter image description here

失退 2025-01-29 23:54:18

我认为您不需要将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{ $0 }.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())
    }
}

I don't think you need to convert a Bool to an Int to get the total score. You could get the score using this simple approach:

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{ $0 }.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())
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文