UILabel中的Int未达到11

时间:2019-06-10 09:53:48

标签: ios swift xcode

我有一个nihoshim UILabel,他的最后一个字符为0,并且我想在else语句为真并被调用时每次更新一次,一切正常,直到达到10,然后突然等于1,

 guard var yoo = Int(nihoshim.text!.suffix(1)) else {
        return
    }
    var alephbet = ["א","ב","ג","ד","ה","ו","ז","ח","ט","י","כ","ל","מ","נ","ס","ע","פ","צ","ק","ר","ש","ת"]
    nihoshim.text! = "ניחושים: " + String(yoo)
    if gameLabel.text!.count <= 3 && (sender.tag == 18 || sender.tag == 20) {
        gameLabel.text = gameLabel.text! + alephbet[alpha.index(after: tag) - 1]
        sender.isHidden = true
    } else if sender.tag == 10 && !(gameLabel.text?.contains("יי"))!{

        sender.isHidden = false
        gameLabel.text = gameLabel.text! + alephbet[alpha.index(after: tag) - 1]
    }
    else {
        yoo += 1
        nihoshim.text! = "ניחושים: " + String(yoo)
        sender.isHidden = true
        print(yoo)
    }

当我打印它时,即使yoo的值也毫无理由地变为0。当我将其更新为yoo += 20时,我单击了几次,仍然点击了20次又单击了1次。

1 个答案:

答案 0 :(得分:1)

问题是您只检查最后一个字符,需要在字符串末尾获取整数,例如:

Nb

在您的代码中,它看起来像这样:

let numberString = nihoshim.text!.split(separator: " ").last ?? nihoshim.text!.suffix(0)