Stan Pak
Stan Pak

Reputation: 119

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:
  [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.

Upvotes: 1

Views: 1817

Answers (1)

Paul.s
Paul.s

Reputation: 38728

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]];

Upvotes: 4

Related Questions