NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

SOLVED: Question: Wesplit breakpoint 1.1 (debug area screenshot and code attached)

Forums > 100 Days of SwiftUI

Here is the screenshot of the debug area. I attached the entire code after the screenshot.

I googled a bit, and used the method I found on youtube to print off the possible error, but still can't get what it means.

For the information, I used "step over " button to go to the upper line (#27) to print off the error. The actually breakpoint was #28 at Form. I don't know if I'm looking at the right place though. I did so (using stepover) from what I learned in this video on debugging tutorial for beginners. https://www.youtube.com/watch?v=qPWfOkHcKdU

I mean, my code is almost identical to Paul's (https://www.hackingwithswift.com/books/ios-swiftui/calculating-the-total-per-person)

I don't see anything wrong with NavigationView or Form. But the printed off error says it is some error about Content

screenshot of breakpoing1.1

Here is the code:

It is completely fine in the canvas (Option + Command +P) , but it gives me green breakpoint 1.1 everytime I run it in the simulateor (command + R)

import SwiftUI

struct ContentView: View {

    @State private var numberOfPeople = 2
    @State private var CheckAmount = 50
    @State private var tipPercentage = 20

    let percentages = [5,10,15,20,25]

    var amountPerPerson: Double {
        let people = Double(numberOfPeople )
        let amount = Double (CheckAmount * (1 + tipPercentage/100))
        let perPerson = amount / people

        return perPerson
       }

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField ("CheckAmount", value: $CheckAmount, format: .currency (code: "USD"))

                    Picker ("number of people", selection:$numberOfPeople){
                        ForEach(2..<100) {
                            Text ("\($0) people ")
                        }
                    }
                }

                Section {
                    Picker ("choose tip percentage", selection: $tipPercentage){
                        ForEach (percentages, id: \.self) {
                            Text (($0), format: .percent)
                        }
                    }
                    .pickerStyle(.segmented)
                }
            header:{
                Text ("Choose Tip Percentage")
               }

                Section {
                    Text ("per person is \(amountPerPerson)")
                }
            }
            }
    }
}

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

1      

I copied and pasted your code and it ran for me, however, the calculation for amountPerPerson is incorrect, it should be let people = Double(numberOfPeople + 2)

var amountPerPerson: Double {
        let people = Double(numberOfPeople + 2)
        let amount = Double (CheckAmount * (1 + tipPercentage/100))
        let perPerson = amount / people

        return perPerson
       }

Alternatively, you could leave let people alone and change your ForEach:

Picker ("number of people", selection:$numberOfPeople){
                        ForEach(2..<100, id: \.self) {
                            Text ("\($0) people ")
                        }
                    }

Otherwise, you end up dividing by zero based on how ForEach works. Without id: \.self, $numberOfPeople gets assigned 0 when 2 is selected in the picker, 1 for 3, 2 for 4, etc...

See here for more info: https://www.hackingwithswift.com/forums/100-days-of-swiftui/foreach-and-picker-and-the-values-it-s-passing/11597

2      

@vtabmow , thank you for trying my code in your Xcode.

Sometime I wonder whether it is the Xcode or MacOS version thing... but I have both updated to the lastest one.

Thanks for pointing out the numberOf People issue. I was aware of it, but I was so distracted by the breakpoint and forgot to correct it.

Have a great day,

Boat

1      

I think Xcode is buggy.

My GuessTheFlag app wouldn't build on my phone yesterday. It would build fine in the simulators. I tried restarting everything and nothing worked. I hadn't made any changes other than add some new flags. I ended up creating a new app, copying and pasting all the code from the old one into the new one and copying over the asset catalog and everything worked fine.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.