Use of OR operator on Cloudkit predicate

孤街浪徒 提交于 2019-12-02 02:05:57

问题


I have a cloudkit record with 5 fields:

Active
User1
User2
User3
User4

I am trying to pick the record where one of the 4 user fields match with the userID variable.

Only one other the 4 user fields will match the userID variable.

This is the code I am using:

var userID = "0984093843897"

predicate = NSPredicate(format: "userID1 = %@ OR userID2 = %@ OR userID3 = %@ OR userID4 = %@ AND active = %@", userID, userID, userID, userID, true)

Basically what I am trying to achieve is something similar to the statement bellow:

if userID1 == userID || userID2 == userID || userID3 == userID || userID4 == userID && active = true

Unfortunatelly cloudkit is returning an error for the predicate format. I am not sure if what I am trying to do here is achievable with cloudkit predicates or if there is a better way for doing it.

Most of the examples I found here on stack deals with predicates with AND operators, not sure how to use an OR operator. Looking at Apple Docs it does not exist, so not sure if there a work around that.


回答1:


Cloud Kit's CKQuery doesn't support OR in a predicate. See the documentation for CKQuery. It shows all the supported predicate operators and while AND and NOT are supported, OR is not.

One possible replacement for OR would be IN. I've only seen this where you wish to see if a single field contains one of several values. But your case is in reverse. Try the following but it may not work.

var userID = "0984093843897"
NSPredicate(format:"%@ IN { %K, %K, %K, %K } AND active = true", userID, "userID1", "userID2", "userID3", "userID4"]


来源:https://stackoverflow.com/questions/37311758/use-of-or-operator-on-cloudkit-predicate

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