Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSPredicate和BEGINSWITH与CloudKit:字段值类型不匹配_Ios_Sql_Nspredicate_Cloudkit - Fatal编程技术网

Ios NSPredicate和BEGINSWITH与CloudKit:字段值类型不匹配

Ios NSPredicate和BEGINSWITH与CloudKit:字段值类型不匹配,ios,sql,nspredicate,cloudkit,Ios,Sql,Nspredicate,Cloudkit,我有一个表“Store”,其属性“name”为String类型(为排序查询和搜索检查索引)。 我想执行一个类似SQL的查询来查找名称以子字符串开头的所有存储 所以我尝试了这个谓词: NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY name BEGINSWITH %@",substring]; CKQuery *query = [[CKQuery alloc]initWithRecordType:@"Store" predic

我有一个表“Store”,其属性“name”为String类型(为排序查询和搜索检查索引)。 我想执行一个类似SQL的查询来查找名称以子字符串开头的所有存储

所以我尝试了这个谓词:

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];
我有一个错误:

<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">
用于查询:
胡乱猜测……试试:

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

这可能是键/值的错误反转,但可能会起作用。

完全不同的方法是创建第三个名为“nameAndCategory”的字段,附加两个字符串并将其添加到字段中。然后了解如何使用谓词进行全文搜索(标记化字符串搜索):

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


但也许唯一可靠的方法是进行两次搜索并合并结果。

我的猜测是,您需要去掉“ANY”

ANY和某些聚合运算符可以与IN和 包含用于执行列表成员资格测试的运算符


因此,ANY是针对列表(数组)的。由于'name'是一个字符串,它不是一个列表,因此出现了不匹配错误:“字段'name'的查询谓词中的字段值类型不匹配”

对您有效吗?因为我有这个错误:**由于未捕获的异常'CKException'终止应用程序,原因:'意外表达式:ANY{“name”,“category”}以“mysubstring”开头如果我使用[NSPredicate predicateWithFormat:@“ANY name以'mysubstring'开头”,它不会返回performQuery错误,但不会返回记录,如果我将其与CKQueryOperation一起使用,它将返回一个错误:对于查询:我简化了问题并打开了一个悬赏来解决CKError。我简化了问题并打开了一个悬赏来解决CKError。好的,谢谢,但是我如何执行查询以查找以“子字符串”开头的“名称”或“类别”的所有商店“约翰,我不确定你是否能直接做到。您可以按照另一个答案的建议尝试全文搜索,也可以创建一个字符串数组的新字段。然后可以使用ANY:“ANY searchableFields BEGINSWITH%@”,其中searchableFields是记录中的一个新字段,它是一个包含两个元素的数组:name和category。
[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"self contains '%@'",substring];