I’m a new user of SwiftUI and Xcode 13.2. I’m taking a course on Udemy, but hit a roadblock when there was an error that said "Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project". It was on the line that said var body: some View {}
. This is the entire code.
import SwiftUI
struct ContentView: View {
@State var userText:String = ""
@State var mode:Int = 1
var body: some View {
VStack {
if mode == 1 {
Text(userText.capitalized())
.font(.largeTitle)
} else if mode == 2 {
Text(userText.lowercased())
.font(.largeTitle)
} else {
Text(userText.uppercased())
.font(.largeTitle)
}
}
TextField("Enter text here...", text: $userText)
.background(Color.yellow)
.padding()
Text(userText)
.font(.largeTitle)
HStack{
Button(action: {mode = 1}) {
RoundedButton(text: "Capitalize", color: .green)
.padding(5)
}
Button(action: {mode = 2}) {
RoundedButton(text: "Lower", color: .blue)
}
Button(action: {mode = 3}) {
RoundedButton(text: "All Caps", color: .red)
.padding(5)
}
}
}
}
struct RoundedButton: View {
var text:String
var color:Color
var body: some View {
Text(text)
.bold()
.frame(maxWidth:.infinity)
.padding()
.background(color)
.foregroundColor(.black)
.cornerRadius(10)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewInterfaceOrientation(.portrait)
}
}
How do I fix this?
2
Answers
The issue has to do with the condition found in your first
if
condition.SwiftUI does not like the use of
.capitalized()
In your third condition, you are using
.uppercased()
. Maybe that is what you meant to use instead?To answer the question, the fix for the issue would be to remove the
.capitalized()
the function
capitalized()
is a function that does not exist in swift.There is however a
capitalized(with locale: Locale?)
see this dochttps://developer.apple.com/documentation/foundation/nsstring/1414023-capitalized
for explanation.
To capitalized a string you can use the method
capitalized
, such astheString.capitalized
. Without the ending()
.So the error you get is due to using a nonexistent function. Replace
capitalized()
withcapitalized
and that will fix your error.In your code use the following to fix your error and capitalize your string in the Text: