CherryBeginner CherryBeginner - 3 months ago 24
iOS Question

Error: Value was defined but never used; consider replacing with a boolean test

I have a function that is getting the following error:


Value intVal was defined but never used; consider replacing with a boolean test.


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
}


Could anyone help me work out what I need to do to get rid of the error?

Answer

Just remove the let and do the comparison on the result of Int directly. You're creating intVal for no reason, and it's complaining that that's an unused variable.

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
}