问题
I'm using CloudKit and wish to perform a search for records based on their string fields.
Apple docs say this is the way to do a tokenized search of a record's fields:
To perform a tokenized search of a record’s fields, use the special operator self. A tokenized search searches any fields that have full-text search enabled, which is all string-based fields by default. Listing 5 shows an example that searches the fields of the record for the token strings bob and smith. Each distinct word is treated as a separate token for the purpose of searching. Comparisons are case- and diacritic-insensitive. These token strings may be found in a single field or in multiple fields but all of the tokens must be present in a record for it to be considered a match.
Listing 5: Matching a field containing a tokenized string
NSPredicate predicate = nil; predicate = [NSPredicate predicateWithFormat:@"self contains 'bob smith'"];
When I enter this exact string for the predicate, I get the an exception.
Code:
predicate = [NSPredicate predicateWithFormat:@"self contains 'bob smith'"];
query = [[CKQuery alloc] initWithRecordType:kCKRecord_Level predicate:predicate];
Exception:
*** Terminating app due to uncaught exception 'CKException', reason: 'Unexpected expression: SELF CONTAINS "bob smith"'
Any ideas what could be wrong? Has anyone had any success with that predicate string and CloudKit?
https://developer.apple.com/library/prerelease/ios/documentation/CloudKit/Reference/CKQuery_class/index.html
回答1:
It looks like the 'self contains' does not work anymore. You still do a tokenized search using this predicate:
NSPredicate(format: "allTokens TOKENMATCHES[cdl] %@", "bob smith")
来源:https://stackoverflow.com/questions/30155215/cloudkit-unexpected-expression-for-nspredicate