user1007522
user1007522

Reputation: 8118

Filter NSArray with NSPredicate gives always null

I'm having the following core data entities:

enter image description here

Now I have NSArray with all POI entities.

I want to filter that array with a predicate so that it searches on every attribute it has to see if it contains something that the user searches.

For example: Array object 0 : address = Test details = Test1 ..

Array object 1: Address = Ba details = Ba1 ..

and the users searches for the 'e', I want to get an nsarray with only object 0.

I tried like this:

NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

 searchResults = [_dataArray filteredArrayUsingPredicate:resultPredicate];

But this returns always nothing.

Can someone help me out on this?

Upvotes: 2

Views: 136

Answers (3)

BHASKAR
BHASKAR

Reputation: 1201

Try this

NSPredicate *resultPredicate = [NSPredicate
                            predicateWithFormat:@"SELF contains[cd] \'%@\'",
                            searchText];

searchResults = [_dataArray filteredArrayUsingPredicate:resultPredicate];

Upvotes: 0

Amir iDev
Amir iDev

Reputation: 1257

Please try like this;

[NSPredicate predicateWithFormat:@"SELF.propertyName contains[cd] %@", searchText];

Upvotes: 3

Akhilrajtr
Akhilrajtr

Reputation: 5182

Since the _dataArray contains POI entities and searching on address attribute, change the predicate to

NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"address contains[cd] %@",
                                searchText];

 searchResults = [_dataArray filteredArrayUsingPredicate:resultPredicate];

Upvotes: 0

Related Questions