簡體   English   中英

var body 上的“無法生成表達式診斷”錯誤:SwiftUI Xcode 13.2 中的一些視圖

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

我是 SwiftUI 和 Xcode 13.2 的新用戶。 我正在學習關於 Udemy 的課程,但是當出現“無法生成表達式診斷;請提交錯誤報告 ( https://swift.org/contributing/#reporting-bugs ) 和包括項目”。 上面var body: some View {} 這是整個代碼。

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

我該如何解決?

該問題與您在第一個if條件中發現的條件有關。

Text(userText.capitalized())

SwiftUI 不喜歡使用.capitalized()

在您的第三種情況下,您正在使用.uppercased() 也許這就是你打算使用的?

要回答這個問題,解決這個問題的方法是刪除.capitalized()

利用:

 Text(userText.capitalized)

沒有 ()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM