randombits
randombits

Reputation: 48440

Swift 3: Int is not convertible to Bool in bitwise operation

Not sure what I'm doing wrong here, but here's the trivial code that's breaking:

if 10 & (1<<18) {
    return
}

This gives me:

'Int' is not convertible to 'Bool'

What is wrong?

Upvotes: 4

Views: 5987

Answers (2)

Naishta
Naishta

Reputation: 12353

Swift 4 : Check that

  1. Your method has a correct return type which the caller is expecting eg: func yourMethod() -> Bool
  2. The caller method has parentheses () at the end yourMethod()

Upvotes: 0

Code Different
Code Different

Reputation: 93151

Unlike in C where you can write...

if (x) { }

... which is really a non-zero check:

if (x != 0) { }

You must test for a boolean condition in Swift. Add != 0 to your statement:

if 10 & (1<<18) != 0 {
    return
}

Upvotes: 13

Related Questions