[英]Swift predicate only matches first value in array of values
I have a class Download
that serves as a wrapper for CKQueryOperation
.我有一个类Download
作为CKQueryOperation
的包装器。 One of the inits allows me to build my predicate with an array of values:其中一个初始化允许我用一组值构建我的谓词:
init(type: String, queryField: String, queryValues: [CKRecordValue], to rec: RecievesRecordable, from database: CKDatabase? = nil) {
let predicate = NSPredicate(format: "\(queryField) = %@", argumentArray: queryValues)
query = CKQuery(recordType: type, predicate: predicate)
reciever = rec
db = database
super.init()
}
When I test it, query
only matches the first value in the array.当我测试它时, query
只匹配数组中的第一个值。 So if queryValues = [testValue0, testValue1]
and I have one record whose field matches testValue0
and I have a second record that matches testValue1
, only the first record will be discovered.因此,如果queryValues = [testValue0, testValue1]
并且我有一条记录,其字段与testValue0
匹配,而我有第二条记录与testValue1
匹配,则只会发现第一条记录。 If I switch the order, than the other record gets recognized.如果我切换顺序,则其他记录会被识别。
It seems weird that I can create a predicate with an array but only the first value gets matched.我可以用数组创建谓词但只匹配第一个值,这似乎很奇怪。 The documentation says that values are substituted in the order they appear, but shouldn't it still be moving on to the second value?文档说值按它们出现的顺序替换,但它不应该继续移动到第二个值吗?
For more context, each record is stored in a separate database (private vs public) and my Download
class launches two separate CKQueryOperations
that both rely on query
, if database
param is left nil.对于更多上下文,每条记录都存储在一个单独的数据库中(私有与公共),如果database
参数为 nil,我的Download
类将启动两个单独的CKQueryOperations
,它们都依赖于query
。 Whichever op fails ends up finding no results that match the first value and then giving up before checking the second value.无论哪个操作失败,最终都找不到与第一个值匹配的结果,然后在检查第二个值之前放弃。
I can include the full code for 'Download' and my failing unit test if needed.如果需要,我可以包含“下载”的完整代码和失败的单元测试。
You are using the wrong predicate.您使用了错误的谓词。 You want to use the IN
operation.您要使用IN
操作。 And instead of using string substitution for the field name, use the %K
format specifier:而不是使用字符串替换字段名称,使用%K
格式说明符:
let predicate = NSPredicate(format: "%K IN %@", arguments: queryField, queryValues)
Note that the NSPredicate(format:argumentArray:)
expects there to be one format specifier in the format string for each value in the argument array.请注意, NSPredicate(format:argumentArray:)
期望参数数组中的每个值的格式字符串中有一个格式说明符。 Since your format only had one format specifier, only the first value was taken from the array.由于您的格式只有一个格式说明符,因此仅从数组中获取第一个值。 And since you used =
, the field was simply compared against that one value.并且由于您使用了=
,因此该字段仅与该值进行了比较。
Basic Comparisons =, == The left-hand expression is equal to the right-hand expression.基本比较=, == 左边的表达式等于右边的表达式。
=, => The left-hand expression is greater than or equal to the right-hand expression. =, => 左侧表达式大于或等于右侧表达式。 <=, =< The left-hand expression is less than or equal to the right-hand expression. <=, =< 左边的表达式小于或等于右边的表达式。 The left-hand expression is greater than the right-hand expression.左侧表达式大于右侧表达式。 < The left-hand expression is less than the right-hand expression. < 左侧表达式小于右侧表达式。 !=, <> The left-hand expression is not equal to the right-hand expression. !=, <> 左边的表达式不等于右边的表达式。
String Comparisons字符串比较
BEGINSWITH The left-hand expression begins with the right-hand expression. BEGINSWITH 左边的表达式以右边的表达式开始。
CONTAINS The left-hand expression contains the right-hand expression. CONTAINS 左边的表达式包含右边的表达式。
ENDSWITH The left-hand expression ends with the right-hand expression. ENDSWITH 左边的表达式以右边的表达式结束。
LIKE The left hand expression equals the right-hand expression: ? LIKE 左手表达式等于右手表达式: ? and和
- are allowed as wildcard characters, where ?允许作为通配符,在哪里? matches 1 character and * matches 0 or more characters.匹配 1 个字符,* 匹配 0 个或多个字符。
MATCHES The left hand expression equals the right hand expression using a regex-style comparison according to ICU v3 (for more details see the ICU User Guide for Regular Expressions).匹配 根据 ICU v3 使用正则表达式样式比较左手表达式等于右手表达式(有关更多详细信息,请参阅 ICU 正则表达式用户指南)。
I used Basic Comparisons我使用了基本比较
let resultPredicate = NSPredicate(format: "%K = %@", "key", "value")
let result_filtered = MYARRAY.filtered(using: resultPredicate) as NSArray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.