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

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

2      

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.

2      

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

2      

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

2      

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

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.