ios - 警告 : Value was defined but never used; consider replacing with a boolean test

标签 ios swift function

我有一个函数收到以下警告:

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
}

谁能帮我弄清楚我需要做什么来消除错误?

最佳答案

只需删除 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
}

关于ios - 警告 : Value was defined but never used; consider replacing with a boolean test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39060215/

相关文章:

ios - 应用程序提供的自定义字体不起作用

ios - 获取包含按下​​的 UIButton 的自定义 UITableViewCell

C - 在函数中重新分配指针类型的结构成员变量。 Valgrind 报告 "Invalid read/write of size 1"

excel - VBA 错误函数 InstrRev = Instr

ios 通话不可用

iOS SDK : Difference between UIButton setTitleForState and UIButton titleLabel. 文本

javascript - Phonegap BarcodeScanner.js 无法在 ios 上关闭

ios - 应用程序运行时不显示自定义原型(prototype)单元格

ios - 从磁盘读取大文件

java - 如何使一组用户定义的函数可用于许多其他类?