开发者

about searching by using NSPredicate

开发者 https://www.devze.com 2023-04-02 04:25 出处:网络
This is the structure of my class ContentItem @interface ContentItem : NSObject { //properties of this class

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

精彩评论

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

关注公众号