2016-08-21 75 views
0

我有越來越以下警告功能:警告:值已定義,但從未使用;考慮用布爾測試替代

價值INTVAL定義,但從未使用過;考慮用布爾測試替換。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
{ 
    text = (timerTxtFld.text! as NSString).stringByReplacingCharactersInRange(range, withString: string) 
    if let intVal = Int(text) { 
     timerDoneBtn.alpha = 1 
     timerDoneBtn.enabled = true 
    } else { 
     timerDoneBtn.enabled = false 
    } 
    return true 
} 

誰能幫我找出我需要做才能擺脫錯誤的?

+2

這是一個警告,而不是一個錯誤......所有這是說爲的是不使用您創建(INTVAL)的值。如果Int(text){}'不能編譯 – penatheboss

回答

3

只要刪除let,並直接對Int的結果做比較。您無故創建intVal,並且抱怨說這是一個未使用的變量。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
{ 
    text = (timerTxtFld.text! as NSString).stringByReplacingCharactersInRange(range, withString: string) 
    if Int(text) != nil 
    { 
     timerDoneBtn.alpha = 1 
     timerDoneBtn.enabled = true 
    } 
    else 
    { 
     timerDoneBtn.enabled = false 
    } 
    return true 
} 
+0

Ooops忘記了這個值。 –

+0

,那麼你可以用_替換它,並且它會使警告消失 – Almo

+1

感謝您的幫助! – CherryBeginner

2

這不是一個錯誤,它是一個警告。編譯器告訴你,你創建了const intVal,但從未使用它。

只要改變你的if語句

if Int(text) != nil 
{ 

} 
相關問題