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?
CodePudding user response:
The issue has to do with the condition found in your first if condition.
Text(userText.capitalized())
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()
CodePudding user response:
use:
Text(userText.capitalized)
without the ()
