Reputation: 2112
I'm trying to search for CloudKit records containing search text entered by my users.
I have records with these titles (in String format):
I have tried using 'contains':
let predicate = NSPredicate(format: "self contains %@", searchText)
let query = CKQuery(recordType: "ShareableItem", predicate: predicate)
publicDatabase.perform(query, inZoneWith: nil) { results, error in
...
But this only returns the results I'm looking for if I use a complete word as a search term.
EG. If I search for 'Item', I get back Item 1,Item 2 and Item 3 but if I search for 'tem' I don't get any results.
Is there a way of including a wildcard character in the search or another way of using partial search text?
I've also tried:
NSPredicate(format: "allTokens TOKENMATCHES[cdl] %@", searchText)
but with the same result
Upvotes: 1
Views: 780
Reputation: 13127
You can only search for partial texts in from the beginning of a field. For instance you could use:
NSPredicate(format: "YourTextField BEGINSWITH %@", searchText)
The token matches query searches for tokens (words) in your record. It will always search for complete words. So far I have only found one workaround for this issue and that is that you take the field where you want to search in en generate an extra field with all the partial tokens that you can generate. For instance if you have a field that contains the text search text
then you will generate your tokens field containing s se sea sear searc e ea ear earc earch a ar arc arch r rc rch c ch t te tex e ex ext x xt
. These will then all be treated as separate tokens.
Upvotes: 2