Jannik S.
Jannik S.

Reputation: 901

Swift: Compare AnyObject with `is` Syntax

I want to check if my sender is an Xyz-Object

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
let senderIsBonusProduct = sender is Xyz

but I get following error:

Could not find a user-defined conversion from type 'Int1' to type 'Bool'

Upvotes: 7

Views: 3522

Answers (3)

gui_dos
gui_dos

Reputation: 1211

Workarounds aren't required anymore with the Beta 3 release and you can combine the is operator with other logical operators.

Upvotes: 1

benjipetit
benjipetit

Reputation: 96

You can also change it to

if let senderOfTypeXYZ = sender as? Xyz {
    // senderOfTypeXYZ is available with the expected type here
 }

Upvotes: 2

GoZoner
GoZoner

Reputation: 70155

The expression sender is Xyz is returning a Bool depending on if sender is of type Xyz. It appears that there is a compiler bug whereby sender is Xyz is actually returning a Int1 that is not getting coerced internally to a Bool.

A workaround is:

let bonus = (sender is Xyz ? true : false)

Upvotes: 5

Related Questions