WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

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.)

   

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

1      

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.

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.