2015-10-03 70 views
0

我試圖檢查的文本字段是空的,但我得到的錯誤「類型(布爾,布爾,布爾)不符合協議‘布爾型’」檢查,如果文本字段爲空

if(userEmail == "", userPassword == "", userRepeatPassword == "") { 


     alertMessage("All fields are required") 
     return 

    } 

我使用的Xcode 7

回答

4

試試這個,

if(userEmail == "" || userPassword == "" || userRepeatPassword == "") 
{ 
       //Do Something 
} 

(或)

if(userEmail == "" && userPassword == "" && userRepeatPassword == "") 
{ 
       //Do Something 
} 
+0

我得到一個線程1:上斷點1.1'如果(USEREMAIL == 「」 || userPassword的== 「」 || userRepeatPassword == 「」 ) { //做點什麼 }'我能做些什麼來解決這個問題? – Ace

+0

使用此'if(userEmail.text ==「」|| userPassword.text ==「」|| userRepeatPassword.text ==「」){// Do Something}'(或)如果您開發Swift 2.0,然後使用此'if(userEmail.text!==「」|| userPassword.text!==「」|| userRepeatPassword.text!==「」){// Do Something}' –

0

你應該使用&&就象這樣:

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") 
} 
1
if (userEmail?.isEmpty || userConfirmEmail?.isEmpty || userPassword?.isEmpty || userConfirmPassword?.isEmpty){ 

     alertMessage("All fields are required!"); 
     return; 
    } 

使用此方法

0

這是爲了檢查一個文本框爲空或不是最簡單的方法:

例如:

if let userEmail = UserEmail.text, !userEmail.isEmpty, 
let userPassword = UserPassword.text, !userPassword.isEmpty, 
let userRepeatPassword = UserRepeatPassword.text, !userRepeatPassword.isEmpty { ... } 

如果所有條件都滿足,那麼所有變量都解開。

0

在Swift 3.0之後,最好使用早期綁定語句,以使代碼更清晰。

guard !(userEmail.isEmpty)! && !(userPassword.isEmpty)! && !(userRepeatPassword.isEmpty)! else { 
     return 
} 

//做一些事情,如果字段包含文本