检查文本字段是否为空
Checking if text field is empty
我正在尝试检查文本字段是否为空,但收到 "Type (Bool, Bool, Bool) does not conform protocol 'Boolean Type' "
错误
if(userEmail == "", userPassword == "", userRepeatPassword == "") {
alertMessage("All fields are required")
return
}
我正在使用 xcode 7
试试这个,
if(userEmail == "" || userPassword == "" || userRepeatPassword == "")
{
//Do Something
}
(或)
if(userEmail == "" && userPassword == "" && userRepeatPassword == "")
{
//Do Something
}
你应该这样使用 &&
:
let userEmail = "William"
let userPassword = "Totti"
let userRepeatPassword = "Italy"
if(userEmail == "" && userPassword == "" && userRepeatPassword == "") {
print("okay")
}
然而,还有另一种方法可以做到,那就是:
if(userEmail.isEmpty && userPassword.isEmpty && userRepeatPassword.isEmpty){
print("okay")
}
另一种方法是像这样检查字符数:
if(userEmail.characters.count == 0 && userPassword.characters.count == 0 && userRepeatPassword.characters.count == 0){
print("okay")
}
if (userEmail?.isEmpty || userConfirmEmail?.isEmpty || userPassword?.isEmpty || userConfirmPassword?.isEmpty){
alertMessage("All fields are required!");
return;
}
使用这个方法
这是检查文本字段是否为空的最简单方法:
例如:
if let userEmail = UserEmail.text, !userEmail.isEmpty,
let userPassword = UserPassword.text, !userPassword.isEmpty,
let userRepeatPassword = UserRepeatPassword.text, !userRepeatPassword.isEmpty { ... }
如果所有条件都为真,则所有变量都被展开。
Swift3.0以后,最好使用早期的绑定语句,让代码更清晰。
guard !(userEmail.isEmpty)! && !(userPassword.isEmpty)! && !(userRepeatPassword.isEmpty)! else {
return
}
// 如果字段包含文本则执行某些操作
我正在尝试检查文本字段是否为空,但收到 "Type (Bool, Bool, Bool) does not conform protocol 'Boolean Type' "
错误 if(userEmail == "", userPassword == "", userRepeatPassword == "") {
alertMessage("All fields are required")
return
}
我正在使用 xcode 7
试试这个,
if(userEmail == "" || userPassword == "" || userRepeatPassword == "")
{
//Do Something
}
(或)
if(userEmail == "" && userPassword == "" && userRepeatPassword == "")
{
//Do Something
}
你应该这样使用 &&
:
let userEmail = "William"
let userPassword = "Totti"
let userRepeatPassword = "Italy"
if(userEmail == "" && userPassword == "" && userRepeatPassword == "") {
print("okay")
}
然而,还有另一种方法可以做到,那就是:
if(userEmail.isEmpty && userPassword.isEmpty && userRepeatPassword.isEmpty){
print("okay")
}
另一种方法是像这样检查字符数:
if(userEmail.characters.count == 0 && userPassword.characters.count == 0 && userRepeatPassword.characters.count == 0){
print("okay")
}
if (userEmail?.isEmpty || userConfirmEmail?.isEmpty || userPassword?.isEmpty || userConfirmPassword?.isEmpty){
alertMessage("All fields are required!");
return;
}
使用这个方法
这是检查文本字段是否为空的最简单方法:
例如:
if let userEmail = UserEmail.text, !userEmail.isEmpty,
let userPassword = UserPassword.text, !userPassword.isEmpty,
let userRepeatPassword = UserRepeatPassword.text, !userRepeatPassword.isEmpty { ... }
如果所有条件都为真,则所有变量都被展开。
Swift3.0以后,最好使用早期的绑定语句,让代码更清晰。
guard !(userEmail.isEmpty)! && !(userPassword.isEmpty)! && !(userRepeatPassword.isEmpty)! else {
return
}
// 如果字段包含文本则执行某些操作