【问题标题】:type error of Closure containing a declaration cannot be used with result builder 'ViewBuilder'包含声明的 Closure 的类型错误不能与结果生成器 \'ViewBuilder\' 一起使用
【发布时间】:2023-01-11 17:52:15
【问题描述】:
    var body: some View {
        VStack(alignment: .center) {
            func CheckInput() { //<-- here the error
                if(name == "") {
                    self.showAlertC1 = true
                }
                else if(!checkInput(name)) {
                    self.showAlertC2 = true
                }
                else {
                       NavigationLink(destination: DisplayView(name: name)){
                                Text("Tap Me")
                            }
                        )
                }
            }
            Text("enter you name")
            TextField("Enter your name...", text: $name, onEditingChanged: { [weak self] (editing) in
                        if !editing {
                            self?.checkInput()
                        }
                    })
        }
        .alert(isPresented: $showAlertC1) {
            Alert(title: Text("Error"), message: Text("Input is empty"), dismissButton: .default(Text("OK")))
        }
        .alert(isPresented: $showAlertC2) {
            Alert(title: Text("Error"), message: Text("Input is not valid"), dismissButton: .default(Text("OK")))
        }
    }

所以我得到了错误(包含声明的闭包不能与结果生成器“ViewBuilder”一起使用)

基本上,如果用户在完成键入或单击“输入键盘”后,它应该转到 func 并检查输入,如果有任何错误应该弹出警报解释问题

@State private var name: String = "" @State private var showAlertC1 = false @State private var showAlertC2 = false

func checkInput(_ input: String) -&gt; Bool { let alphabet = CharacterSet.letters return input.rangeOfCharacter(from: alphabet) != nil }

【问题讨论】:

  • 为什么func CheckInput() { ... }VStack(...) { ... here ... }里面?它应该与var body: some View { ... }处于同一“级别”吗?

标签: swift


【解决方案1】:

你不能把这样的东西放在一个视图中:

   func CheckInput() {
                if(name == "") {
                    self.showAlertC1 = true
                }
                else if(!checkInput(name)) {
                    self.showAlertC2 = true
                }
                else {
                       NavigationLink(destination: DisplayView(name: name)){
                                Text("Tap Me")
                            }
                        )
                }
            }

首先,您不能将 func 放入 var 中!

第二:所有你放在一个视图中的东西都应该返回一个视图。 NavigationLink 是一个视图,但 self.showAlertC1 和 self.showAlertC2 不是视图。

请按照一些教程学习如何使用 SwiftUI。

你的代码应该是这样的:

@State private var nameIsValid = false
    var body: some View {
        VStack(alignment: .center) {
            if nameIsValid {
                NavigationLink(destination: DisplayView(name: name)){
                    Text("Tap Me")
                })
            }
            Text("enter you name")
            TextField("Enter your name...", text: $name, onEditingChanged: { [weak self] (editing) in
                if !editing {
                    self?.checkInput()
                }
            })
        }
        .alert(isPresented: $showAlertC1) {
            Alert(title: Text("Error"), message: Text("Input is empty"), dismissButton: .default(Text("OK")))
        }
        .alert(isPresented: $showAlertC2) {
            Alert(title: Text("Error"), message: Text("Input is not valid"), dismissButton: .default(Text("OK")))
        }
    }
    
func checkInput(name: String) {
    if (name.isEmpty) {
        self.showAlertC1 = true
    } else if(!isValid(name)) {
        self.showAlertC2 = true
    } else {
        nameIsValid = true
    }
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多