简体   繁体   English

Swift 谓词仅匹配值数组中的第一个值

[英]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

More information : Predicate Programming Guide 更多信息:谓词编程指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 匹配数组中的字段仅在值位于数组的第一行时匹配 - Match on field in array only matches when the value is in the first row of the array MongoDB 查询仅更新匹配条件的第一个数组值 - MongoDB Query to Update Only The First Array Value That Matches Criteria 遍历数组只会在Swift Playground中获得第一个值 - Iterating through an array only gets the first value in Swift Playground javascript.filter 只匹配第一个数组项 - javascript .filter only matches first array item 如何创建谓词来过滤 Swift 中具有关联值的枚举数组? - How to create a predicate to filter array of enums with associated values in Swift? 在一个元素的第一个实例匹配相同字母(仅)值之后,删除数组中的所有元素 - Remove all elements in an array after the first instance of an element that matches same alphabetic (ONLY) value 数组值只返回第一个字符和额外的值 - Array value only returning first character and extra values 在数组中查找与Swift中的特定条件匹配的第一个元素 - Find first element in array that matches a specific condition in Swift 如果第一列中的值与指定值匹配,则在数组中查找最大值 - find largest value in an array if value in first column matches specified value Swift:仅当数组匹配特定条件时如何执行操作? - Swift : How to perform actions only if an array matches specific conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM