Checking if text fields are empty cause error in Swift 2

南笙酒味 提交于 2019-12-02 15:54:08

问题


I am trying to check if a textbox has no value.

When I do this:

if(userEmail?.isEmpty || userPassword?.isEmpty || userPasswordRepeat?.isEmpty)  

I get the following error

I tried adding "?" before the ".isEmpty" but the error won't go away

Any ideas?


回答1:


Try this....

if txtEmail.text?.isEmpty == true || txtPassword.text?.isEmpty == true || txtRePassword.text?.isEmpty == true{
        print("true")
}



回答2:


If interested also in positive case, the following is an alternative solution for Swift 2:

let email = self.txtEmail.text where !email.isEmpty, let password = self.txtPassword.text where !password.isEmpty {
    //all fields are not nil && not empty
}else{
    //some field is nil or empty
}


来源:https://stackoverflow.com/questions/33051996/checking-if-text-fields-are-empty-cause-error-in-swift-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!