The problem of comparing UIColors in Swift

I need to compare two UIColors, but for some reason it always returns false. I tried comparing with == and .isEqual() , but none of them work.

 //This is a sample of the colors I have created let blue_color = UIColor(red: 122/255, green: 180/255, blue: 190/255, alpha: 1) //This is the SpriteNode I have to compare let square = SKSpriteNode(color: randomColorController(), size: ksquaresize) 

randomColorController () is just a function that randomizes colors and returns it, so it is called when a square is created.

 func randomColorController() -> UIColor { let random = arc4random_uniform(3) + 1 switch random { case 1: let color = blue_color return color case 2: let color = yellow_color return color case 3: let color = yellow_color return color default: let color = UIColor.clearColor() return color } 

Then, depending on the position of the square that I created, he will check the collision by comparing the color of the created square and the colors that I initialized at the beginning.

 func checkCollision(currentTime: CFTimeInterval, Square: SKSpriteNode) -> Int{ let color = Square.color print(color.isEqual(blue_color)) print(color.isEqual(red_color)) print(color.isEqual(yellow_color)) if Square.position.y >= 0 && Square.position.y <= 40 { if color.isEqual(blue_color) && (Square.position.x < basesize.width) { // ADDS 1 POINT TO THE SCORE LABEL flag = 1 points += 1 } else if color.isEqual(red_color) && (Square.position.x > (basesize.width*2)){ flag = 1 points += 1 } else if color.isEqual(yellow_color) && (Square.position.x < (basesize.width*2)) && (Square.position.x > basesize.width) { flag = 1 points += 1 } else { flag = -1 } } 

But color.isEqual(blue_color) or any other color does not seem to work. I printed Square.color and blue_color (and others) and they match. But it will always return false .

+5
source share
2 answers

You can use "==" or isEqual. I just checked both of them and they work great:

 let redColor = UIColor.redColor() let greenColor = UIColor.greenColor() let blueColor = UIColor.blueColor() let testColor = UIColor.greenColor() println( testColor == redColor ) // false println( testColor == greenColor ) // true println( testColor == blueColor ) // false println( testColor.isEqual(redColor) ) // false println( testColor.isEqual(greenColor) ) // true println( testColor.isEqual(blueColor) ) // false 

I just reproduced the problem only after highlighting the SKSpriteNode color and, as you said, only with the colors of the fractions. You can solve this problem by comparing the color description as follows:

 let blue_color = UIColor(red: 122/255, green: 180/255, blue: 190/255, alpha: 1) let yellow_color = UIColor(red: 253/255, green: 213/255, blue: 123/255, alpha: 1) let red_color = UIColor(red: 238/255, green: 116/255, blue: 71/255, alpha: 1) func randomColorController() -> UIColor { let random = arc4random_uniform(3) + 1 switch random { case 1: return blue_color case 2: return red_color case 3: return yellow_color default: return UIColor.clearColor() } } let square = SKSpriteNode(color: randomColorController(), size: CGSize(width: 30, height: 30)) if square.color.description == blue_color.description { println(true) } if square.color.description == red_color.description { println(true) } if square.color.description == yellow_color.description { println(true) } 
+5
source
 extension UIColor { func isEqual(color: UIColor?) -> Bool { guard let color = color else { return false } var red:CGFloat = 0 var green:CGFloat = 0 var blue:CGFloat = 0 var alpha:CGFloat = 0 self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) var targetRed:CGFloat = 0 var targetGreen:CGFloat = 0 var targetBlue:CGFloat = 0 var targetAlpha:CGFloat = 0 color.getRed(&targetRed, green: &targetGreen, blue: &targetBlue, alpha: &targetAlpha) return (Int(red*255.0) == Int(targetRed*255.0) && Int(green*255.0) == Int(targetGreen*255.0) && Int(blue*255.0) == Int(targetBlue*255.0) && alpha == targetAlpha) } } 

I tested in fast 3, 4

+4
source

All Articles