If the statement does not work for all three digits?

advertisements

So here's the simple problem.

 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)
    }

The last if statement isn't working at ALL, while the others are working just as they should. I'm really confused as to why this is the case.


First of all, in your code you're comparing strings (not numbers contained in strings) using < and > operators. That is probably not what you want to do. According to the Swift docs:

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

The comparison results for strings are different than if you just compared the numbers, so for example the expression "2" > "10" is true, while of course 2 > 10 is false.

If you want to compare the numbers contained in the strings, you can do this instead:

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

Also, it would help you avoid bugs if you wrote your if conditions like this:

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

In your current code, if one of the conditions is incorrect, more than one block of code can be executed for the same value of cell.count.text.