Discovered on the Swift-User list that UIColor in the most recent May 3rd toolchain supports initializers for both Float and CGFloat. Which is odd:
You can see both initializers at the bottom of the screen shot.
Bit of a problem because Xcode complains about an “Ambiguous use of init(red:green:blue:alpha:)”. No kidding. So right now, you can start casting everything, e.g.
let color = UIColor(red: CGFloat(0.5), green: CGFloat(0.5), blue: CGFloat(0.5), alpha: CGFloat(1.0))
Or you can do something really stupid like this:
extension Double { var cg: CGFloat { return CGFloat(self) } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let c = UIColor(red: 0.5.cg, green: 0.5, blue: 0.5, alpha: 0.5) } }
It works but ew.
So here’s my question: who ever uses Float
with UIColor
to begin with and why did this pop up in the latest build? Is it part of the Cocoa auto-migration thing? And do you think I should just go ahead and file a bug?
Update: Bug filed. SR-1479 Resolved as dupe. 🙂
3 Comments
Yes and yes
Definitely seems like a migration thing. and definitely seems like a bug 🙂 I would expect those types to eventually be interchangeable, but who knows…
Since when you’re working on an application you probably use a certain palette of colors what I do is this
import UIKit
/** A simple representation of color. */
public struct ColorStyleUtils {
let uiColor: UIColor
/** Constructs a color from RGB and alpha values. Arguments range from 0.0 to 1.0. */
public init(red: Double, green: Double, blue: Double, alpha: Double = 1.0) {
uiColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))
}
public static var someColor: ColorStyleUtils { return ColorStyleUtils(red: 125.0/255.0, green: 125.0/255.0, blue: 125.0/255.0, alpha: 1.0) }
}
Then on my others objects I just call the predefined color.
someButton.backgroundColor = ColorStyleUtils.someColor