swift NSPredicate logical OR

一世执手 提交于 2019-12-02 06:45:36

问题


I've got a single string substitution working with NSPredicate but returning core data records that contain either StringA or StringB doesn't seem to be something I can figure out. I want something like this:

let filter = NSPredicate(format: ("%K = %@", "type", "StringA") OR ("%K = %@", "type", "StringB"))

But of course that doesn't work. Help?


回答1:


You have to specify a format string, followed by a comma-separated list of arguments to substitute into the format:

let filter = NSPredicate(format:"%K = %@ OR %K = %@", "type", "StringA", "type", "StringB")

If the keys are not reserved words and do not contain special characters then you can specify them directly in the format string:

let filter = NSPredicate(format:"type = %@ OR type = %@", "StringA", "StringB")


来源:https://stackoverflow.com/questions/26480292/swift-nspredicate-logical-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!