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

前端 未结 2 796
醉梦人生
醉梦人生 2021-01-19 19:29

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:

2条回答
  •  粉色の甜心
    2021-01-19 19:53

    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
    }
    

提交回复
热议问题