Swift UIColor initializer - compiler error only when targeting iPhone5s

前端 未结 2 1300
旧巷少年郎
旧巷少年郎 2020-11-30 13:53

The Situation:

Working from a new project: iOS > Application > Game > SpriteKit, Swift, iPhone

Here I\'ve written a function in my G

相关标签:
2条回答
  • 2020-11-30 14:12

    It probably has to do with a mismatch between Float and Double. Basically, if the arguments are expected to be CGFloats, you need to pass CGFloats.

    let color = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))
    

    What's important to understand here is that CGFloat is defined as Float on 32 bit platforms, and Double on 64 bit platforms. So, if you're explicitly using Floats, Swift won't have a problem with this on 32 Bit platforms, but will produce an error on 64 bit.

    0 讨论(0)
  • 2020-11-30 14:14

    See from the source code you should use CGFloat instead of Float

    enter image description here

    this should work

    func colorize (hex: Int, alpha: Double = 1.0) -> UIColor {
        let red = Double((hex & 0xFF0000) >> 16) / 255.0
        let green = Double((hex & 0xFF00) >> 8) / 255.0
        let blue = Double((hex & 0xFF)) / 255.0
        var color: UIColor = UIColor( red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha:CGFloat(alpha) )
        return color
    }
    
    0 讨论(0)
提交回复
热议问题