Reputation: 52510
The following code is giving me an error in Xcode 8.2.1:
UIColor(red: redSwitch.isOn ? 1 : 0, green: greenSwitch.isOn ? 1 : 0, blue: blueSwitch.isOn ? 1 : 0 )
The error is:
'Int1' is not convertible to 'Bool'
Why is that? redSwitch.isOn
works fine in an if
statement condition. The Apple reference documentation says UISwitch.isOn
returns Bool
.
How do I get this to work?
Upvotes: 2
Views: 1541
Reputation: 4209
Another take on this bug.
import CoreGraphics
let hit: Bool = true
let returnStroke: Bool = false
let rotation: CGFloat = (hit ? 90 : 120)
rotation *= returnStroke ? -1.0 : 1.0 // 'Int1' is not convertible to 'Bool'
The real problem is that rotation
should be a variable and not a constant. Changing let
to var
makes the "convertible" error disappear, and the code will run.
While keeping rotation
as a constant, no amount of parenthesis in the right side expression would make the correct error message appear, either. That message being "Left side of mutating operator isn't mutable..."
Upvotes: 0
Reputation: 80781
It's a rubbish error message, but the problem is simply that you're missing the alpha:
parameter from UIColor
's init(red:green:blue:alpha:)
initialiser.
let color = UIColor(red: redSwitch.isOn ? 1 : 0, green: greenSwitch.isOn ? 1 : 0,
blue: blueSwitch.isOn ? 1 : 0, alpha: 1)
From what I can tell, the source of the weird error message seems to be the use of the ternary operator in a convenience
initialiser call where you're missing one of the parameters.
A more minimal example would be:
class Foo {
convenience init(a: Int, b: Int) {}
}
let f = Foo(a: true ? 1 : 0) // 'Int1' is not convertible to 'Bool'
I went ahead and filed a bug over this error, SR-3839.
Upvotes: 3