Search code examples
swiftintegerbooleantoggle

Change Bool value to Int


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

Solution

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