开发者

about searching by using NSPredicate

This is the structure of my class ContentItem

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

an NSArray has above class's objects. I want to search the objects by searching words. So I tried to do this.

NSArray *filteredItem = [array filteredArrayUsingPredicate:
  开发者_JAVA技巧[NSPredicate predicateWithFormat: @"SELF contains[cd] %@", searchString]];

result:

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

Actually I don't know how to search the objects. Can anyone help?

Thanks.


I think you need to access the property you are intending to search on. See the Apple docs for Using Predicates with Key-Paths

e.g.

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

for both properties

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜