gpt4 book ai didi

swift - react 性 cocoa 验证

转载 作者:行者123 更新时间:2023-11-30 13:19:44 27 4
gpt4 key购买 nike

如何在执行 RAC 命令或单击按钮后验证表单?我的 View 模型中有这个

self.executeLoginRacCommand = RACCommand(enabled: activateButtonSignal, signalBlock: { (any : AnyObject!) -> RACSignal! in

return self.executeLoginAPI()
})

//My question is here, how can I display an alert to my view controller saying may email is invalid.
//MARK: - Execute Login API
private func executeLoginAPI() -> RACSignal {
if self.isEmailValid(self.email) {
return self.signInServices.signInAPIService.signInAPISignal(self.email, password: self.password)
} else {
self.errorMessage = "Please insert a valid email"
return RACSignal.empty()
}
}

//MARK: - Is Email Valid
func isEmailValid(email: String) -> Bool {
let emailRegEx = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"

let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluateWithObject(email)
}

我在 View Controller 中访问它,如下所示:

 self.signInButton.rac_command = self.signInViewModel.executeLoginRacCommand

您能否建议一种验证无效电子邮件并在单击按钮后显示警报的好方法?提前致谢!

最佳答案

在您的 else 中分支,您将返回一个空的 RACSignal。相反,返回发送错误的 RACSignal ( RACSignal.error(<the error>) )。

在您的 ViewController 中,您可以订阅errors self.executeLoginRacCommand的属性(property)并向用户显示错误。

关于swift - react 性 cocoa 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37892077/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com