Xcode 8 Beta 4 CGColor.components unavailable

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

    @Hamish's answer works, but my original intent was not to use UIColor or NSColor so that my code works in both iOS & MacOS. @LeoDabus suggested using SKColor, but that's just a type alias to either NSColor or UIColor, and doesn't have a direct init from CGColor anyway, however, Leo's suggestion prompted me to refine my kludge using CIColor instead:

    import CoreImage
    
    extension CGColor {
        var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
            let ciColor = CIColor(cgColor: self)
            return (ciColor.red, ciColor.green, ciColor.blue, ciColor.alpha)
        }
    }
    

提交回复
热议问题