在Cloudkit谓词上使用OR运算符 [英] Use of OR operator on Cloudkit predicate

查看:81
本文介绍了在Cloudkit谓词上使用OR运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含5个字段的cloudkit记录:

I have a cloudkit record with 5 fields:

Active
User1
User2
User3
User4

我正在尝试选择4个用户字段之一的记录与userID变量匹配。

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

4个用户字段中只有另一个与userID变量匹配。

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

不幸的是,cloudkit返回谓词格式错误。我不确定使用Cloudkit谓词是否可以实现我在这里要做的事情,或者是否有更好的方法来做到这一点。

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.

我在堆栈中找到的大多数示例都使用AND运算符处理谓词,但不确定如何使用OR运算符。查看Apple Docs尚不存在,因此不确定是否可以解决此问题。

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.

推荐答案

Cloud Kit的 CKQuery 在谓词中不支持 OR 。请参阅 CKQuery 的文档。它显示了所有受支持的谓词运算符,并且支持 AND NOT ,而 OR

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.

一个可能替代 OR 的可能是 IN 。我只在希望查看单个字段是否包含多个值之一的地方查看过此内容。但是您的情况恰恰相反。尝试以下操作,但可能不起作用。

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"]

这篇关于在Cloudkit谓词上使用OR运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆