gpt4 book ai didi

swift - 如果语句不适用于三位数?

转载 作者:行者123 更新时间:2023-11-28 11:15:35 25 4
gpt4 key购买 nike

这是一个简单的问题。

 if cell.count.text == "\(0)"
{
cell.pText.textColor = UIColor.grayColor()
cell.nicebutton!.setImage(UIImage(named:"defaulltup"), forState: UIControlState.Normal)
cell.nopebutton!.setImage(UIImage(named:"defaultdown"), forState: UIControlState.Normal)
cell.count.textColor = UIColor.grayColor()
cell.time.textColor = UIColor.grayColor()
}
if cell.count.text >= "\(1)"
{
cell.pText.textColor = UIColor.whiteColor()
//cell.time.textColor = UIColor(red: 42.0/255, green: 204.0/255, blue: 113.0/255, alpha: 1)
cell.count.textColor = UIColor(red: 42.0/255, green: 204.0/255, blue: 113.0/255, alpha: 1)
cell.nicebutton!.setImage(UIImage(named:"upgreen"), forState: UIControlState.Normal)
cell.nopebutton!.setImage(UIImage(named:"defaultdown"), forState: UIControlState.Normal)
//cell.nopebutton!.setImage(UIImage(named:"downred"), forState: UIControlState.Normal)
cell.count.textColor = UIColor(red: 42.0/255, green: 204.0/255, blue: 113.0/255, alpha: 1)
}
if cell.count.text < "\(0)"
{
cell.pText.textColor = UIColor(red: 231.0/255, green: 76.0/255, blue: 50.0/255, alpha: 1)
//cell.time.textColor = UIColor(red: 231.0/255, green: 76.0/255, blue: 50.0/255, alpha: 1)
cell.count.textColor = UIColor(red: 231.0/255, green: 76.0/255, blue: 50.0/255, alpha: 1)
cell.nicebutton!.setImage(UIImage(named:"defaulltup"), forState: UIControlState.Normal)
//cell.nicebutton!.setImage(UIImage(named:"upgreen"), forState: UIControlState.Normal)
cell.nopebutton!.setImage(UIImage(named:"downred"), forState: UIControlState.Normal)

}
if cell.count.text >= "\(99)"
{

cell.pText.textColor = UIColor(red: 249.0/255, green: 191.0/255, blue: 59.0/255, alpha: 1)
//cell.nopebutton!.setImage(UIImage(named:"downred"), forState: UIControlState.Normal)
cell.nicebutton!.setImage(UIImage(named:"golden"), forState: UIControlState.Normal)
cell.count.textColor = UIColor(red: 249.0/255, green: 191.0/255, blue: 59.0/255, alpha: 1)
}

最后一个 if 语句根本不起作用,而其他语句却能正常工作。我真的很困惑为什么会这样。

最佳答案

首先,在您的代码中,您使用 < 比较字符串(不是 字符串中包含的数字)和 >运营商。这可能不是您想要做的。根据 Swift 文档:

For characters in strings, “greater than” means “appears later in the alphabet than”

字符串的比较结果与仅比较数字的结果不同,例如表达式 "2" > "10"是真的,当然2 > 10是假的。

如果你想比较字符串中包含的数字,你可以这样做:

if cell.count.text.toInt() == 0
// etc.

此外,如果您编写了 if,它会帮助您避免错误像这样的条件:

if condition1 {
//...
}
else if condition2 {
//...
}
else {
//...
}

在您当前的代码中,如果其中一个条件不正确,则可以针对 cell.count.text 的相同值执行多个代码块。 .

关于swift - 如果语句不适用于三位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com