2013-04-27 92 views
0

我試圖用NSPredicate過濾array,但總是返回empty array使用NSPredicate過濾數組

代碼:

_contentFilteredArray = [_contentArray mutableCopy]; 
[_contentFilteredArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"special == 1"]]; // Always return an empty array. 

原始_contentArray:

(
    { 
    id = 1; 
    special = 0; 
}, 
    { 
    id = 2; 
    special = 1; 
}, 
    { 
    id = 3; 
    special = 1; 
}, 
    { 
    id = 4; 
    special = 0; 
    } 
) 

我希望_contentFilteredArray:

(
    { 
    id = 2; 
    special = 1; 
}, 
    { 
    id = 3; 
    special = 1; 
}, 
) 
+1

這應該工作,如果值是NSNumbers。試試這個日誌,看看它給了你什麼:NSLog(@「%@」,[_ contentArray [1] [@「special」] class]); – rdelmar 2013-04-28 05:47:36

回答

1

試試這個,更沿用傳統的withFormat方法。

[NSPredicate predicateWithFormat:@"special == %@", @1] 
2

確保數組中的項目是鍵值編碼兼容的關鍵special

[item valueforKey: @"special"] 

返回預期的數值?