I have been facing this problem in the newest Xcode version, Version 14.3.1.
I was following the codes from Hacking With Swift, 100 Days of SwiftUI. While following the guy’s code, I faced the problem: " failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)." I rechecked my code with the video, ChatGPT, BardAI, and a bunch of other tools. It kept giving me that error. I reported to Apple right now, but I have no idea how to fix is, so I need your help.
//
// ContentView.swift
// WeSplit2
//
// Created by Oliver Park on 2023/07/04.
//
import SwiftUI
struct ContentView: View {
@State private var checkAmount = 0.0
@State private var numberOfPeople = 2
@State private var tipPercentage = 20
let tipPercentages = [10, 15, 20, 25, 0]
var body: some View {
NavigationView {
Form {
Section {
TextField("Amount", value: $checkAmount, format: .currency(code: Locale.current.currency?.identifier ?? "USD"))
.keyboardType(.decimalPad)
Picker("Number of people", selection: $numberOfPeople) {
ForEach(2..<100) {
Text("($0) people")
}
}
}
Section {
Picker("Tip percentage", selection: $tipPercentage) {
ForEach(tipPercentages, id: .self) { percentage in
Text(String(percentage), format: .percent)
}
}
}
Section {
Text(checkAmount, format: .currency(code: Locale.current.currency?.identifier ?? "USD"))
}
}
.navigationTitle("WeSplit")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
2
Answers
It seems you’re facing an issue with the newest Xcode version, Version 14.3.1, while following the code from Hacking With Swift, specifically the "100 Days of SwiftUI" tutorial. The error message you encountered states: "failed to produce diagnostic for expression; please submit a bug report." You have already reported the issue to Apple but are seeking assistance on how to resolve it.
Upon reviewing the code you provided, I noticed a potential issue. The problem might be related to the TextField initializer and its usage of the .currency format specifier.
To address this, you can try modifying the code by explicitly specifying the expected value type for checkAmount as a Double and using the NumberFormatter class to format the currency input. Here’s an updated version of your code:
Make sure to replace the existing TextField line with the updated code.
Additionally, ensure that you have imported the Foundation framework in your file:
import Foundation
The problem is that your code doesn’t match what was used in the tutorial. Specifically, this:
Should really be this:
I hope that helps!