2016-10-11 24 views
0

在iOS 10.0.1和Xcode 8.0上,我有一個CKQuery,它成功地爲各種條件(BEGINSWITH等於某些字符串等)返回數據。現在我正在專門查找其中一個字段中具有空值的記錄。使用NSPredicate搜索NIL的CKQuery始終引發無效表達式錯誤

Apple's Docs,加上How do I set up a NSPredicate to look for objects that have a nil attributeNSPredicate with boolean is not comparing a boolean to NO,下面的語法應搜索零值:

NSPredicate *searchConditions = [NSPredicate predicateWithFormat:@"joiner = nil "]; 
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"availableURLs" 
              predicate:searchConditions]; 

當我設置一個斷點,並檢查searchConditions,它的計算結果joiner == nil。當我嘗試使用它,第二行拋出異常:

Terminating app due to uncaught exception 'CKException', reason: 
'Invalid predicate: joiner == nil (Error Domain=CKErrorDomain Code=12 
"Invalid right expression in <joiner == nil>: <nil> is not a function expression" 
UserInfo={ck_isComparisonError=true, 
NSLocalizedDescription=Invalid right expression in <joiner == nil>: <nil> is not a function expression, 
NSUnderlyingError=0x1706516d0 {Error Domain=CKErrorDomain Code=12 "<nil> is not a function expression" 
UserInfo={NSLocalizedDescription=<nil> is not a function expression, ck_isComparisonError=true}}})' 

我試過的===nilNilNIL,並且NULL每個組合。全部評估爲joiner == nil並拋出異常。

在第一個鏈接的問題中,OP使用predicateWithSubstitutionVariables代替[NSNull null]。我也嘗試過,但它再次評估爲joiner == nil,並拋出了相同的異常。

蘋果的文檔有部分比較特別在尋找零,我看不出他們爲榜樣,從我的不同:

predicate = [NSPredicate predicateWithFormat:@"firstName = nil"]; 

缺少什麼我在這裏?

+1

我認爲這個問題更多的是關於'CKQuery'的侷限性,'NSPredicate',這可以解釋爲什麼你可以在'NSArray'中使用'nil'作爲自定義對象......這並不清楚,但是從'CKQuery'文件:'構建您的謂詞 NSPredicate對象定義了用於確定記錄是否與查詢匹配的邏輯條件。 CKQuery類僅支持由完整NSPredicate類提供的一部分謂詞行爲。# – Larme

+0

@Larme謝謝,我只是更仔細地閱讀了ckquery文檔。我以前錯過了關於只支持一個子集的一點,如果nspredicate。如果您將該帖子作爲答案發布,我會接受並可能幫助未閱讀文檔的下一個人足夠接近。 – Thunk

回答

2

根據CKQuery docCKQuery似乎不允許nil謂詞(在撰寫答案時在iOS 10中)。

建立你的謂詞
一個NSPredicate對象定義用於確定記錄是否爲 查詢的匹配的邏輯條件。 CKQuery類僅支持由完整NSPredicate類提供的謂詞 行爲的一個子集。

您的NSPredicate構造似乎沒有錯,因爲它可以在自定義對象的NSArray上工作。

+0

那麼在使用CKQuery構建謂詞時,還有其他一些方法來測試nil嗎? –

相關問題