2011-09-04 27 views
1

搜索這是我班的上述類的對象結構ContentItem有關使用NSPredicate

@interface ContentItem : NSObject { 
    //properties of this class 
    NSString *name; 
    NSString *type; 
... 
} 

一個NSArray了。 我想通過搜索單詞來搜索對象。 所以我試圖做到這一點。

NSArray *filteredItem = [array filteredArrayUsingPredicate: 
    [NSPredicate predicateWithFormat: @"SELF contains[cd] %@", searchString]]; 

結果:

Can't use in/contains operator with collection 
     <ContentItem: 0x68309b0> (not a collection) 

其實我不知道如何尋找的對象。 任何人都可以幫忙嗎?

謝謝。

回答

4

我想你需要訪問你打算搜索的財產。請參閱Apple文檔Using Predicates with Key-Paths

例如,

NSArray *filteredItem = [array filteredArrayUsingPredicate: 
    [NSPredicate predicateWithFormat: @"SELF.name contains[cd] %@", searchString]]; 

這兩個屬性

NSArray *filteredItem = [array filteredArrayUsingPredicate: 
    [NSPredicate predicateWithFormat: @"SELF.name contains[cd] %@ OR SELF.type contains[cd] %@", searchString, searchString]]; 
+0

Paul.s //然後纔可能進行搜索,不僅「名」,但同時所有屬性? –

+0

希望有人能給出更好的解決方案,但是你可以用另一個條件來擴展謂詞。查看更新的答案。 –

+0

非常感謝Paul! –