简体   繁体   English

类型“()”不能符合 SWIFT 上的“视图”

[英]Type '()' cannot conform to 'View' on SWIFT

I'm getting this error when I try to validate password field under a SecureTextField field.当我尝试验证 SecureTextField 字段下的密码字段时出现此错误。

any thoughts?有什么想法吗? I did similar with an TextField for Email validation and works just fine!我对用于 Email 验证的 TextField 进行了类似的操作,并且工作正常! my issue is with the password validation "If"我的问题是密码验证“如果”

                HStack {
                
                    TextField("Email Address", text: $loginVM.credentials.email) { isEditing in
                        self.isEditing = isEditing
                        
                        errorMessage = ""
                        ////// == getting  email validation boolean after losing  the focus
                        if  isEditing  == false  {
                            if validateEmail(emailAddressString: loginVM.credentials.email) ==  true {
                                errorMessage = "YES, Email is Good"
                                print(errorMessage)
                                } else {
                                errorMessage = "*-Oops,Something is wrong with your email !!!"
                                print(errorMessage)

                            }
                        }
                       
                    }
                   
                    } .textFieldStyle(MyTextFieldStyle(focused: $isEditing))
                    
                        
                HStack {
                    
                    SecureTextField(text: $loginVM.credentials.password)

                            .padding(10)
                            .focused($isInFocus)
                            .background(
                            RoundedRectangle(cornerRadius: 10, style: .continuous)
                            .stroke(isInFocus ? Color.orange : Color.gray, lineWidth: 1))
                    
                               
                            }.disableAutocorrection(true)
                // Here is where I'm getting this Type '()' cannot conform to 'View'
                            if validatePassword(passwordString: loginVM.credentials.password) ==  true {
                                errorMessage = "YES, Password  OK"
                                print(errorMessage)
                                } else {
                                errorMessage = "*-Oops,Something is wrong with your Password !!!"
                                print(errorMessage)

                            }

you can wrap your logic inside onSubmit .您可以将逻辑包装在onSubmit中。

the compiler expects some View inside the ViewBuilder编译器期望 ViewBuilder 中有一些 View

SecureTextField(text: $name)
    .padding(10)
    .focused($isInFocus)
    .disableAutocorrection(true)
    .background(
        RoundedRectangle(
            cornerRadius: 10,
            style: .continuous
        )
        .stroke(isInFocus ? Color.orange : Color.gray, lineWidth: 1)
    )
    .onSubmit {
        if validatePassword(passwordString: loginVM.credentials.password) {
            errorMessage = "YES, Password  OK"
            print(errorMessage)
        } else {
            errorMessage = "*-Oops,Something is wrong with your Password !!!"
            print(errorMessage)
        }
    }

the .onSubmit block will execute when the user presses submit button on the keyboard though i would recommend handling this logic inside a view model and execute the logic once everything is set up .onSubmit块将在用户按下键盘上的提交按钮时执行,但我建议在视图 model 中处理此逻辑并在一切设置好后执行逻辑

暂无
暂无

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

相关问题 类型 'Void' 不能符合 'View' | Swift - Type 'Void' cannot conform to 'View' | Swift 类型“()”不能符合 Swift 中的“AccessibilityRotorContent”? - Type '()' cannot conform to 'AccessibilityRotorContent' in Swift? Swift:“类型 'any Hashable' 不能符合 'Hashable'” - Swift: "Type 'any Hashable' cannot conform to 'Hashable'" Swift 错误:“类型 '()' 不能符合 'View';只有 struct/enum/class 类型可以符合协议”调用函数写入文本时 - Swift error: "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols" when calling function to write text 类型“任何视图”不能符合具有泛型的协议上的“视图” - Type 'any View' cannot conform to 'View' on protocol with generics 类型“ SomeVC”不符合协议“ NextLevelDelegate”(与Swift 4.1.2编译) - Type 'SomeVC' cannot conform to protocol 'NextLevelDelegate' (compiled with Swift 4.1.2) 协议类型“Any”的值不能符合“Equatable”Swift - Value of protocol type 'Any' cannot conform to 'Equatable' Swift navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 类型 '()' 不能符合 'View'; 只有 struct/enum/class 类型才能符合协议; 使用 SwiftUI 调用函数 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols; calling functions with SwiftUI 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM