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

Strange error while attempting WeSplit project

Forums > 100 Days of SwiftUI

I got this xcode error during work on the extra credits for this project: Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project. Can anyone tell me what I did that may have caused this?? I am lost... 😞

Sorry new to this coding thing . It shows up as a code line error. This is the code that produces the error :

//
//  ContentView.swift
//  WeSplit2
//
//  Created by Rodney Pemberton on 10/29/21.
//

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, 10, 15, 20, 25, 30]

    var totalPerPerson : Double {
        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 body: some View {
        NavigationView{
            Form {
                Section {
                    TextField("Amount", value: $checkAmount, format:
                                    .currency(code: Locale.current.currencyCode ?? "USD"))
                        .keyboardType(.decimalPad)
                        .focused($amountisFocused)
                    Picker("Number of people", selection: $numberOfPeople) {
                        ForEach(1..<101) {
                            Text("\($0) people")
                        }
                        }
                    }
                Section {
                    Picker("Tip percentage", selection: $tipPercentage) {
                        ForEach(tipPercentages, id: \.self) {
                            Text($0, format: .percent)
                        }
                    }
                    .pickerStyle(.menu)
                } header: {
                    Text("How much tip \"percentage\" do you wnat to leave.")
                }
                Section {
                    Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "USD"))
                    }
            }
            .navigationTitle("We split tip")
            .toolbar {
                Spacer()
                ToolbarItemGroup(placement:  .keyboard) {
                    Button("Completed") {
                        amountisFocused = false
                    }
                }
            }
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

// // ContentView.swift // WeSplit2 // // Created by Rodney Pemberton on 10/29/21. //

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, 10, 15, 20, 25, 30]

var totalPerPerson : Double {
    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 body: some View {
    NavigationView{
        Form {
            Section {
                TextField("Amount", value: $checkAmount, format:
                                .currency(code: Locale.current.currencyCode ?? "USD"))
                    .keyboardType(.decimalPad)
                    .focused($amountisFocused)
                Picker("Number of people", selection: $numberOfPeople) {
                    ForEach(1..<101) {
                        Text("\($0) people")
                    }
                    }
                }
            Section {
                Picker("Tip percentage", selection: $tipPercentage) {
                    ForEach(tipPercentages, id: \.self) {
                        Text($0, format: .percent)
                    }
                }
                .pickerStyle(.menu)
            } header: {
                Text("How much tip \"percentage\" do you wnat to leave.")
            }
            Section {
                Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "USD"))
                }
        }
        .navigationTitle("We split tip")
        .toolbar {
            Spacer()
            ToolbarItemGroup(placement:  .keyboard) {
                Button("Completed") {
                    amountisFocused = false
                }
            }
        }
    }
}

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

1      

It's very hard to say without seeing some code. Most likely, though, is something in your View body is not correct. Post it so we can see and we'll be able to tell you more.

1      

Is this a Log (in debug area) report? Does the app responed as expected? If so please read On Log Noise

1      

It your toolbar and the Spacer() is before the ToolbarItemGroup need to be after.

it should be

.toolbar {
    ToolbarItemGroup(placement:  .keyboard) {
        Spacer()
        Button("Completed") {
            amountIsFocused = false
        }
    }
}

1      

Thanks @NigelGee, that did the trick. I don't know how I missed that but I did. 🤔

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, 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!

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.