Xcode 8 Beta 4 CGColor.components unavailable

前端 未结 5 1726
醉梦人生
醉梦人生 2021-01-21 07:43

Whilst the code below worked previously, it has stopped working in Xcode 8 Beta 4, presumably because the components return was a very un-Swift-y C-array of floats,

5条回答
  •  误落风尘
    2021-01-21 08:17

    I may be mistaking something, but you can find this in the imported header of CGColor.

    /* Return the color components (including alpha) associated with `color'. */
    
    @available(OSX 10.3, *)
    public var __unsafeComponents: UnsafePointer? { get }
    

    Isn't this what you are looking for?

    I can write something like this:

    public var cgColour: CGColor {
        get {
            return CGColor(red: self.colourRed, green: self.colourGreen, blue: self.colourBlue, alpha: self.colourAlpha)
        }
        set {
            if let comps = newValue.__unsafeComponents, newValue.numberOfComponents == 4 {
                self.colourRed = comps[0]
                self.colourGreen = comps[1]
                self.colourBlue = comps[2]
                self.colourAlpha = comps[3]
            }
        }
    }
    

    It works as I expect, but I'm not sure it's as you expect, or Apple would treat this as using a private API. (Apple's latest documentation of CGColor does not contain double-underscore leaded symbols.)

提交回复
热议问题