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

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      

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.