Search code examples
iosswiftnspredicatecloudkit

NSPredicate case insensitive string search with CloudKit


I'm trying to search a String field of a CloudKit object. The following line successfully compiles and runs without error.

NSPredicate(format: "Title BEGINSWITH %@", "Apple")

Yet, when I try add case insensitive search, it the app crashes.

NSPredicate(format: "Title BEGINSWITH[cdl] %@", "Apple")

With error:

*** Terminating app due to uncaught exception 'CKException', reason: 'Invalid predicate: Title BEGINSWITH[cdl] "Apple" (Error Domain=CKErrorDomain Code=12 "Invalid operator in <Title BEGINSWITH[cdl] "Apple">: Unsupported operator: BEGINSWITH" UserInfo=0x7fb69d02e910 {ck_isComparisonError=true, NSLocalizedDescription=Invalid operator in <Title BEGINSWITH[cdl] "Apple">: Unsupported operator: BEGINSWITH, NSUnderlyingError=0x7fb69d02e9e0 "Unsupported operator: BEGINSWITH"})'

Solution

  • As from the documentation on CKQuery: "String-based comparisons are case insensitive but otherwise all comparisons must be an exact match of the specified value."

    https://developer.apple.com/library/ios/documentation/CloudKit/Reference/CKQuery_class/index.html#//apple_ref/occ/cl/CKQuery

    So I think BEGINSWITH is all you should need. Have you not found this to be the case?