0
有一次,我問如何將1個關鍵字傳遞給多個鍵,並得到了這樣的答案:Use 1 search value for multiple keywords。然後,我問如何通過密鑰作爲參數:Pass the key as param in NSPredicate我可以使用`predicateWithSubstitutionVariables`或類似的東西來代替鍵值嗎?
所以現在,我很好奇,如果我可以做反之亦然:我可以通過多個鍵作爲參數與1(或許多)搜索話?如果是的話,它會是怎樣的?
一些代碼供你們編輯。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(($key = 'value1')
@" OR ($key = 'value2')) "
@" AND ($key != '0') "];
predicate = [predicate predicateWithSubstitutionVariables:@{@"key": @"myProperty"}];
注意,在斷言,有一個(AND =!),所以我將無法使用IN predicate
:
[NSPredicate predicateWithFormat:@"myProperty IN %@", @[@"value1", @"value2", @"value3"]];
這些只是一些示例代碼,所以請不要不要試圖修正謂詞,但要注意以下問題:substituteVariables
與變量是過濾的關鍵。
您是否嘗試過和沒工作的? – Willeke
它沒有。我使用正常的謂詞('id = 1或id = 2')並在lldb中打印出來,它顯示:'id == 1 OR id == 2'。但是,對樣本使用謂詞時,它會顯示:「」id「== 1或」id「== 2」,並且不會過濾正確的數據。看起來'subtitutionVariables'用一個字符串替換佔位符,而不是一個關鍵字。 – Eddie