简体   繁体   English

使用CloudKit的NSPredicate和BEGINSWITH:字段值类型不匹配

[英]NSPredicate and BEGINSWITH with CloudKit : Field value type mismatch

I have a table "Store" with the attributes "name" of type String (indexes checked for sort query and search). 我有一个表“Store”,其属性为String类型的“name”(为排序查询和搜索检查索引)。 I want to execute a SQL like query to find all stores with name beginning with a substring. 我想执行类似SQL的查询来查找名称以子字符串开头的所有商店。

So I tried this predicate : 所以我尝试了这个谓词:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY name BEGINSWITH %@",substring];

CKQuery *query = [[CKQuery alloc]initWithRecordType:@"Store" predicate:pred];

CKQueryOperation *queryOperation = [[CKQueryOperation alloc] initWithQuery:query];

queryOperation.desiredKeys = @[@"name",@"category"];

NSMutableArray *results = [[NSMutableArray alloc] init];

queryOperation.recordFetchedBlock = ^(CKRecord *record) {
    [results addObject:record];
};
queryOperation.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *error) {
   //my own code
}
[publicDatabase addOperation:queryOperation];

I have this error : 我有这个错误:

<CKError 0x1887a540: "Invalid Arguments" (12/1009); "Field value type mismatch in query predicate for field 'name'"> for query : <CKQuery: 0x18882650; recordType=Store, predicate=ANY name BEGINSWITH "mysubstring">

Wild guess...try: 疯狂的猜测...尝试:

[NSPredicate predicateWithFormat:@"ANY {'name','category'} BEGINSWITH %@",substring]; 

That may be an incorrect reversal of key/value but it might work. 这可能是键/值的错误反转,但它可能有效。

A completely different approach would be to create a third field called "nameAndCategory", append the two strings and add them to the field. 一种完全不同的方法是创建一个名为“nameAndCategory”的第三个字段,追加两个字符串并将它们添加到字段中。 Then figure out how to do the full text search (tokenized string search) with the predicate: 然后弄清楚如何使用谓词进行全文搜索(标记化字符串搜索):

[NSPredicate predicateWithFormat:@"self contains '%@'",substring]; 

or 要么

[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"self contains '%@'",substring];

But perhaps the only sure approach is to do two searches and combine the results. 但也许唯一可靠的方法是进行两次搜索并合并结果。

My guess is that you need to get rid of the "ANY". 我的猜测是你需要摆脱“任何”。

The ANY and SOME aggregate operators may be combined with the IN and CONTAINS operators to perform list membership tests. ANY和SOME聚合运算符可以与IN和CONTAINS运算符组合以执行列表成员资格测试。

So ANY is for lists (Arrays). 所以任何是列表(数组)。 Since 'name' is a String, it is not a list, hence the mismatch error: "Field value type mismatch in query predicate for field 'name'" 由于'name'是String,因此它不是列表,因此不匹配错误:“字段'name'的查询谓词中的字段值类型不匹配”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM