Swift 如果语句不适用于三位数?
这是一个简单的问题Swift 如果语句不适用于三位数?,swift,if-statement,Swift,If Statement,这是一个简单的问题 if cell.count.text == "\(0)" { cell.pText.textColor = UIColor.grayColor() cell.nicebutton!.setImage(UIImage(named:"defaulltup"), forState: UIControlState.Normal) cell.nopebutton!.setImage(UIImage(named:"defaultd
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)
}
如果cell.count.text==“\(0)”
{
cell.pText.textColor=UIColor.grayColor()
cell.nicebutton!.setImage(UIImage(名为:“defaulltup”),用于状态:UIControlState.Normal)
cell.nopebutton!.setImage(UIImage(名为:“defaultdown”),用于状态:UIControlState.Normal)
cell.count.textColor=UIColor.grayColor()
cell.time.textColor=UIColor.grayColor()
}
如果cell.count.text>=“\(1)”
{
cell.pText.textColor=UIColor.whiteColor()
//cell.time.textColor=UIColor(红色:42.0/255,绿色:204.0/255,蓝色:113.0/255,alpha:1)
cell.count.textColor=UIColor(红色:42.0/255,绿色:204.0/255,蓝色:113.0/255,alpha:1)
cell.nicebutton!.setImage(UIImage(名为:“upgreen”),用于状态:UIControlState.Normal)
cell.nopebutton!.setImage(UIImage(名为:“defaultdown”),用于状态:UIControlState.Normal)
//cell.nopebutton!.setImage(UIImage(名为:“downred”),用于状态:UIControlState.Normal)
cell.count.textColor=UIColor(红色:42.0/255,绿色:204.0/255,蓝色:113.0/255,alpha:1)
}
如果cell.count.text<“\(0)”
{
cell.pText.textColor=UIColor(红色:231.0/255,绿色:76.0/255,蓝色:50.0/255,alpha:1)
//cell.time.textColor=UIColor(红色:231.0/255,绿色:76.0/255,蓝色:50.0/255,alpha:1)
cell.count.textColor=UIColor(红色:231.0/255,绿色:76.0/255,蓝色:50.0/255,alpha:1)
cell.nicebutton!.setImage(UIImage(名为:“defaulltup”),用于状态:UIControlState.Normal)
//cell.nicebutton!.setImage(UIImage(名为:“upgreen”),用于状态:UIControlState.Normal)
cell.nopebutton!.setImage(UIImage(名为:“downred”),用于状态:UIControlState.Normal)
}
如果cell.count.text>=“\(99)”
{
cell.pText.textColor=UIColor(红色:249.0/255,绿色:191.0/255,蓝色:59.0/255,alpha:1)
//cell.nopebutton!.setImage(UIImage(名为:“downred”),用于状态:UIControlState.Normal)
cell.nicebutton!.setImage(UIImage(名为:“golden”),forState:UIControlState.Normal)
cell.count.textColor=UIColor(红色:249.0/255,绿色:191.0/255,蓝色:59.0/255,alpha:1)
}
最后一个if语句根本不起作用,而其他语句则在正常工作。我真的不明白为什么会这样。移动此代码:
if cell.count.text >= 99
以上:
if cell.count.text >= 1
移动此代码:
if cell.count.text >= 99
以上:
if cell.count.text >= 1
移动此代码:
if cell.count.text >= 99
以上:
if cell.count.text >= 1
移动此代码:
if cell.count.text >= 99
以上:
if cell.count.text >= 1
您的问题是,您正在比较字符串,并希望它们的行为与整数相同。不幸的是,情况并非如此。您可以轻松地检查以下内容:
print("100" > "99")
您将得到False的输出。就字符而言,9在1之后,因此99总是大于100。我的建议是将cell.count.text转换为Int,然后进行比较。请注意,转换为Int会产生一个可选值,因此必须使用if-let来绑定该值
if let count = cell.count.text.toInt() {
if count < 0 {
// Do something useful here
}
if count > 99 {
// Do something useful here
}
}
else {
// Handle the case where cell.count.text isn't a valid integer
}
如果let count=cell.count.text.toInt(){
如果计数<0{
//在这里做些有用的事
}
如果计数>99{
//在这里做些有用的事
}
}
否则{
//处理cell.count.text不是有效整数的情况
}
您的问题是,您正在比较字符串,并希望它们的行为与整数相同。不幸的是,情况并非如此。您可以轻松地检查以下内容:
print("100" > "99")
您将得到False的输出。就字符而言,9在1之后,因此99总是大于100。我的建议是将cell.count.text转换为Int,然后进行比较。请注意,转换为Int会产生一个可选值,因此必须使用if-let来绑定该值
if let count = cell.count.text.toInt() {
if count < 0 {
// Do something useful here
}
if count > 99 {
// Do something useful here
}
}
else {
// Handle the case where cell.count.text isn't a valid integer
}
如果let count=cell.count.text.toInt(){
如果计数<0{
//在这里做些有用的事
}
如果计数>99{
//在这里做些有用的事
}
}
否则{
//处理cell.count.text不是有效整数的情况
}
您的问题是,您正在比较字符串,并希望它们的行为与整数相同。不幸的是,情况并非如此。您可以轻松地检查以下内容:
print("100" > "99")
您将得到False的输出。就字符而言,9在1之后,因此99总是大于100。我的建议是将cell.count.text转换为Int,然后进行比较。请注意,转换为Int会产生一个可选值,因此必须使用if-let来绑定该值
if let count = cell.count.text.toInt() {
if count < 0 {
// Do something useful here
}
if count > 99 {
// Do something useful here
}
}
else {
// Handle the case where cell.count.text isn't a valid integer
}
如果let count=cell.count.text.toInt(){
如果计数<0{
//在这里做些有用的事
}
如果计数>99{
//在这里做些有用的事
}
}
否则{
//处理cell.count.text不是有效整数的情况
}
您的问题是,您正在比较字符串,并希望它们的行为与整数相同。不幸的是,情况并非如此。您可以轻松地检查以下内容:
print("100" > "99")
您将得到False的输出。就字符而言,9在1之后,因此99总是大于100。我的建议是将cell.count.text转换为Int,然后进行比较。请注意,转换为Int会产生一个可选值,因此必须使用if-let来绑定该值
if let count = cell.count.text.toInt() {
if count < 0 {
// Do something useful here
}
if count > 99 {
// Do something useful here
}
}
else {
// Handle the case where cell.count.text isn't a valid integer
}
如果let count=cell.count.text.toInt(){
如果计数<0{
//在这里做些有用的事
}
如果计数>99{
//在这里做些有用的事
}
}
否则{
//处理cell.count.text不是有效整数的情况
}
首先,在代码中,使用
运算符比较字符串(不是字符串中包含的数字)。这可能不是你想做的。根据Swift文件:
对于字符串中的字符,“大于”表示“大于”
字母比“
字符串的比较结果与仅比较数字时不同,因此例如表达式“2”>“10”
为true,而2>10
当然为false
如果要比较字符串中包含的数字,可以使用d