Xcode 8 Beta 4 CGColor.components unavailable

前端 未结 5 1719
醉梦人生
醉梦人生 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:26

    I'm also a little perplexed as to why they have removed the components property from CGColor, as there doesn't seem to be any kind of obvious replacement method/property. It looks like they're trying to get people to use the higher level UIColor or NSColor classes instead. (As @rickster has discovered, this looks more like a simple transitional issue).

    One solution, as you're working with RGB colors, would be to simply wrap your CGColor in a UIColor/NSColor, and then use the getRed(_:green:blue:alpha:) method to get out the components instead.

    public var cgColour : CGColor {
        get {
            return CGColor(red: colourRed, green: colourGreen, blue: colourBlue, alpha: colourAlpha)
        }
        set {
            NSColor(cgColor: newValue)?.getRed(&colourRed, green: &colourGreen, blue: &colourBlue, alpha: &colourAlpha)
        }
    }
    

    Perhaps not the most ideal solution – would certainly be interested to know if anyone else has a better one, or knows more about this change. Depending on the usage of this property, you may also want to consider simply making it of type UIColor/NSColor to prevent the needless wrapping that this solution requires.

提交回复
热议问题