I've just started migrating some Swift code over to Swift 3. The formatting for color constants has changed from
let color = UIColor.yellowColor() // Swift 2
let color = UIColor.yellow // Swift 3
Most of my project's code has been migrating over just fine using this new syntax. However, some of the UIColor color constants are not working: UIColor.white
, UIColor.red
, UIColor.green
, and UIColor.blue
. All other UIColor constants, like UIColor.yellow
, work just fine. The malfunctioning constants show up in autocomplete, and websites online report that they work (http://leaks.wanari.com/2016/09/26/converting-project-swift-3-0/).
However, when I compile the following file:
import SpriteKit
let aColor = UIColor.black
let aColor2 = UIColor.darkGray
let aColor3 = UIColor.lightGray
let aColor4 = UIColor.white
let aColor5 = UIColor.gray
let aColor6 = UIColor.red
let aColor7 = UIColor.green
let aColor8 = UIColor.blue
let aColor9 = UIColor.cyan
let aColor10 = UIColor.yellow
let aColor11 = UIColor.magenta
let aColor12 = UIColor.orange
let aColor13 = UIColor.purple
let aColor14 = UIColor.brown
let aColor15 = UIColor.clear
let aFakeColor = UIColor.fakeColor
It gives the following errors:
Instance member 'white' cannot be used on type 'UIColor'
Instance member 'red' cannot be used on type 'UIColor'
Instance member 'green' cannot be used on type 'UIColor'
Instance member 'blue' cannot be used on type 'UIColor'
Type 'UIColor' has no member 'fakeColor'
Now the last error makes perfect sense; there is no color constant called fakeColor
. But this shows that the compiler is seeing the malfunctioning color constants, as it's giving a different error.
According to Apple's documentation, the malfunctioning color constants do exist. Why can't my compiler see them?
Update: I found the issue.
I had an extension to UIColor that made it act more Swifty. It allowed accessing the RGB components as shown below. Now that UIColor has
red
,green
, andblue
properties that represent the colors red, green, and blue, there was a conflict.For anyone else that's having a similar issue: make sure to check if you have any extensions that be causing the problem.
Swift 2 Extension
Swift 3 Extension