NSPredicate 调用时出现 NSInvalidArgumentException

NSInvalidArgumentException on NSPredicate call

所以我有以下代码,它给了我一个 NSInvalidArgumentException。

let test = "AND ANY typeId = 25"
fetchRequest.predicate = NSPredicate(format: "latitude BETWEEN {%f,%f} AND longitude BETWEEN {%f,%f} %@", (latitude-0.10), (latitude+0.10), (longitude-0.10), (longitude+0.10), test)

但是,如果我删除 %@ 和 test 元素,调用就会正常进行。我在这里错过了什么? afaik,test 是一个字符串,%@ 是我应该用来引用字符串对象的内容吗?

谢谢!

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html

Important: Use a %@ format specifier only to represent an expression. Do not use it to represent an entire predicate. If you attempt to use a format specifier to represent an entire predicate, the system raises an exception.

您可以试试

let predicate = NSPredicate(format: "latitude BETWEEN {\(latitude-0.1),
\(latitude+0.1)} AND longitude BETWEEN {\(longitude-0.1),\(longitude+0.1)} 
\(test)")