Xcode 9.3 - NSPredicate Bool crash

后端 未结 3 1757
伪装坚强ぢ
伪装坚强ぢ 2021-01-24 15:52

After the Xcode 9.3 update, I\'ve noticed that if you want to have Predicate like this:

let predicate = NSPredicate(format: \"preferred = %@\", true as CVarArg)
         


        
3条回答
  •  臣服心动
    2021-01-24 16:30

    After a bit of investigation, I've discovered how to fix this. In short:

    // Solution 1 [ NSNumber ]
    let bool = NSNumber(booleanLiteral: true)
    let predicate = NSPredicate(format: "preferred = %@", bool as CVarArg)
    
    // Solution 2 [ Bool ] (static example)
    let predicate = NSPredicate(format: "preferred == YES")
    

    As also explained here, it's simply better to deal with Obj-C type instead of Swift type when we have to deal with this kind of methods.

提交回复
热议问题