Evaluate Bool property of optional object in if statement

后端 未结 3 1712
失恋的感觉
失恋的感觉 2020-12-31 13:11

I am looking for a way to evaluate a Swift Bool concisely in a single if statement, when the Bool is the property of an optional objec

3条回答
  •  难免孤独
    2020-12-31 13:39

    Another possible solution is:

    if objectWithBool?.bool ?? false {
        println("objectWithBool != nil && objectWithBool.bool == true")
    } else {
        println("objectWithBool == nil || objectWithBool.bool == false")
    }
    

    The "nil coalescing operator" a ?? b is a shorthand for

    a != nil ? a! : b
    

提交回复
热议问题