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:

enter image description here

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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, and blue 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

//
//  UIColor+Swifty.swift
//
//  Created by Cin316 on 3/6/16.
//  Usage is permitted under the MIT license.
//  This does not work in Swift 3.0 !
//

import SpriteKit

public extension UIColor {

    public var alpha: CGFloat? {
        get {
            var a: CGFloat = 0
            if (self.getWhite(nil, alpha: &a)) {
                return a
            } else {
                return nil
            }
        }
    }

    public var white: CGFloat? {
        get {
            var w: CGFloat = 0
            if (self.getWhite(&w, alpha: nil)) {
                return w
            } else {
                return nil
            }
        }
    }

    public var red: CGFloat? {
        get {
            var r: CGFloat = 0
            if (self.getRed(&r, green: nil, blue: nil, alpha: nil)) {
                return r
            } else {
                return nil
            }
        }
    }
    public var green: CGFloat? {
        get {
            var g: CGFloat = 0
            if (self.getRed(nil, green: &g, blue: nil, alpha: nil)) {
                return g
            } else {
                return nil
            }
        }
    }
    public var blue: CGFloat? {
        get {
            var b: CGFloat = 0
            if (self.getRed(nil, green: nil, blue: &b, alpha: nil)) {
                return b
            } else {
                return nil
            }
        }
    }

    public var hue: CGFloat? {
        get {
            var h: CGFloat = 0
            if (self.getHue(&h, saturation: nil, brightness: nil, alpha: nil)) {
                return h
            } else {
                return nil
            }
        }
    }
    public var saturation: CGFloat? {
        get {
            var s: CGFloat = 0
            if (self.getHue(nil, saturation: &s, brightness: nil, alpha: nil)) {
                return s
            } else {
                return nil
            }
        }
    }
    public var brightness: CGFloat? {
        get {
            var b: CGFloat = 0
            if (self.getHue(nil, saturation: nil, brightness: &b, alpha: nil)) {
                return b
            } else {
                return nil
            }
        }
    }

}

Swift 3 Extension

//
//  UIColor+Swifty.swift
//
//  Created by Cin316 on 3/6/16.
//  Usage is permitted under the MIT license.
//  Notice the addition of "Comp" to conflicting properties.
//

import SpriteKit

public extension UIColor {

    public var alphaComp: CGFloat? {
        get {
            var a: CGFloat = 0
            if (self.getWhite(nil, alpha: &a)) {
                return a
            } else {
                return nil
            }
        }
    }

    public var whiteComp: CGFloat? {
        get {
            var w: CGFloat = 0
            if (self.getWhite(&w, alpha: nil)) {
                return w
            } else {
                return nil
            }
        }
    }

    public var redComp: CGFloat? {
        get {
            var r: CGFloat = 0
            if (self.getRed(&r, green: nil, blue: nil, alpha: nil)) {
                return r
            } else {
                return nil
            }
        }
    }
    public var greenComp: CGFloat? {
        get {
            var g: CGFloat = 0
            if (self.getRed(nil, green: &g, blue: nil, alpha: nil)) {
                return g
            } else {
                return nil
            }
        }
    }
    public var blueComp: CGFloat? {
        get {
            var b: CGFloat = 0
            if (self.getRed(nil, green: nil, blue: &b, alpha: nil)) {
                return b
            } else {
                return nil
            }
        }
    }

    public var hue: CGFloat? {
        get {
            var h: CGFloat = 0
            if (self.getHue(&h, saturation: nil, brightness: nil, alpha: nil)) {
                return h
            } else {
                return nil
            }
        }
    }
    public var saturation: CGFloat? {
        get {
            var s: CGFloat = 0
            if (self.getHue(nil, saturation: &s, brightness: nil, alpha: nil)) {
                return s
            } else {
                return nil
            }
        }
    }
    public var brightness: CGFloat? {
        get {
            var b: CGFloat = 0
            if (self.getHue(nil, saturation: nil, brightness: &b, alpha: nil)) {
                return b
            } else {
                return nil
            }
        }
    }

}