我有一个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次。

最佳答案

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

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

在您的代码中,它看起来像这样:
let numberString = nihoshim.text!.split(separator: " ").last ?? nihoshim.text!.suffix(0)
guard var yoo = Int(numberString) 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)
}

关于ios - UILabel中的Int未达到11,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56524087/

10-13 03:49