简体   繁体   English

var body 上的“无法生成表达式诊断”错误:SwiftUI Xcode 13.2 中的一些视图

[英]'failed to produce diagnostic for expression' error on var body: some View in SwiftUI Xcode 13.2

I'm a new user of SwiftUI and Xcode 13.2.我是 SwiftUI 和 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".我正在学习关于 Udemy 的课程,但是当出现“无法生成表达式诊断;请提交错误报告 ( https://swift.org/contributing/#reporting-bugs ) 和包括项目”。 It was on the line that said var body: some View {} .上面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?我该如何解决?

The issue has to do with the condition found in your first if condition.该问题与您在第一个if条件中发现的条件有关。

Text(userText.capitalized())

SwiftUI does not like the use of .capitalized() SwiftUI 不喜欢使用.capitalized()

In your third condition, you are using .uppercased() .在您的第三种情况下,您正在使用.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()要回答这个问题,解决这个问题的方法是删除.capitalized()

use:利用:

 Text(userText.capitalized)

without the ()没有 ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM