简体   繁体   English

Xcode 9.3-NSPredicate Bool崩溃

[英]Xcode 9.3 - NSPredicate Bool crash

After the Xcode 9.3 update, I've noticed that if you want to have Predicate like this: 在Xcode 9.3更新之后,我注意到,如果您想要这样的谓词:

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

You have a crash. 你崩溃了 But in Xcode 9.2 this wasn't a problem. 但是在Xcode 9.2中,这不是问题。 Any idea? 任何想法?

// Solution 3 [ Apple Documentation ] //解决方案3 [ Apple文档 ]

let predicate = NSPredicate(format: "preferred == TRUE")

The exception occurs because true is not an object ( %@ ). 发生异常是因为true不是对象( %@ )。 You need the %d placeholder 您需要%d占位符

let predicate = NSPredicate(format: "preferred = %d", true)

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. 就像这里已经解释的那样,当我们不得不处理这种方法时,最好用Obj-C类型而不是Swift类型。

我认为您也可以使用此:

NSPredicate(format: "preferred = true")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM