UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

I have a problem.Help me please.

Forums > SwiftUI

Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project

import SwiftUI

struct ContentView: View { @State private var checkAmount = 0.0 @State private var numberOfPeople = 2 @State private var tipPercentage = 20 @FocusState private var amountIsFocused: Bool

let tipPercentages = [0..<101]

var totalPerPerson: Double {
    // calculate the total per person here
    let peopleCount = Double(numberOfPeople + 2)
    let tipSelection = Double(tipPercentage)

    let tipValue = checkAmount / 100 * tipSelection
    let grandTotal = checkAmount + tipValue
    let amountPerPerson = grandTotal / peopleCount

    return amountPerPerson

}

var total: Double {
    let tipSelection = Double(tipPercentage)
    let tipValue = checkAmount / 100 * tipSelection
    let grandTotal = checkAmount + tipValue
    return grandTotal
}
var body: some View {
    NavigationView {
    Form {
        Section {
            TextField("Amount", value: $checkAmount, format: .currency(code: Locale.current.currencyCode ?? "USD"))
                .keyboardType(.numberPad)
                .focused($amountIsFocused)

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

        Section {
            Picker("Tip percentage", selection: $tipPercentage) {
                ForEach(tipPercentages, id: \.self) {
                    Text($0, format: .percent)
                }
            }
            .pickerStyle(.segmented)
        } header: {
            Text("How much tip do you want to leave?")
        }
        Section {
            Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "USD" ))
        }
    }
    .navigationTitle("WeSplit")
    .toolbar {
        ToolbarItemGroup(placement: .keyboard) {
            Spacer()
            Button("Done") {
                amountIsFocused = false
            }
        }
    }
        Section {
            Text(total, format: .currency(code: Locale.current.currencyCode ?? "USD" ))
      }
   }
}

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

1      

Two problems:

  1. You have an extra Section outside the Form there at the bottom of the body.
  2. Your tipPercentages is not correct. You have:
let tipPercentages = [0..<101]

but this makes tipPercentages a type [Range<Int>]. That's not going to work when you put it in a ForEach. You want it to be something like this:

let tipPercentages = [10, 15, 20, 25, 0]

which would be of type [Int].

(If you truly want to allow the user to select any tip percentage from 0 to 100, you can do this:

let tipPercentage = 0..<101

But given that this is a segmented Picker, that seems less than advisable, since the system will try to fit all 101 of those options on one line! You would need to use a regular Picker.)

1      

When posting code to these forums, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

Doing this will ensure that you end up with something like this:

func printSomething(_ thing: String?) {
    if let thing = thing {
        print(thing)
    } else {
        print("nothing there")
    }
}

instead of this:

func printSomething(_ thing: String?) { if let thing = thing { print(thing) } else { print("nothing there") } }

2      

May I add?

This is not a very useful title: I have a problem. Help me please.

Please help us to help you. Provide a useful title. Think of others who read the forum looking for solutions. Your title won't help anyone understand your issue. Instead, they will have to open your post to read the content and wasting their time if their problem isn't related to yours.

When you provide a descriptive title, others can scan posts to quickly weed out questions or problems not related to their search.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.