NSMutableArray and NSPredicate Filtering

I am trying to filter my array with two objects inside an object, for example, I have a Person object in which I have a name, address, number, email address, etc. I am trying to filter a list of arrays of objects with just a name and number. How can this be achieved using NSPredicate?

+7
source share
4 answers

Create a predicate (the following assumes your Person class has the name and number properties of the string):

 NSString *nameFilter = @"Steve*"; NSString *numberFilter = @"555-*"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name like %@) or (number like %@)", nameFilter, numberFilter]; 

Then filter the array (assuming you have an NSArray of Person objects):

 NSArray *personArray = /* obtain from somewhere */; NSArray *filtered = [personArray filteredArrayUsingPredicate:pred]; 

The result will be an array containing Person objects whose name can be "Steve", "Steven", etc., and the number of which begins with 555- .

Edit

What you are saying does not really make sense. You cannot remove properties from a class (or rather should not). If you need an array containing only names and numbers that you will need to iterate through an array of Person objects:

 NSMutableArray *result = [NSMutableArray array]; for (Person *p in personArray) [result addObject:[NSString stringWithFormat:"%@ : %@", [p name], [p number]]]; 
+13
source

I believe what you are looking for:

 NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name==%@",name]; 

or if you want similarities for string names, you can also use:

 NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name like %@",name]; 

and provided that the phone number is just int, you can use ==, <, <=, etc. for comparing numbers

then apply it with:

 NSArray * filteredarray = [array filteredArrayUsingPredicate:predicate]; 
+4
source

I prefer to use the word CONTAINS for filtering. Easy to do this job. Or you can combine them together:

 NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@ OR name LIKE[cd] %@", filterName, filterName]; 

You can refer to the OFFICIAL DOC :

BEGINSWITH: left expression begins with right expression.

CONTAINS: The left expression contains the right expression.

ENDSWITH: left expression ends with right expression.

LIKE: expression of the left hand is equal to the right expression :? and * are allowed as wildcards, where? matches 1 character and * matches 0 or more characters.

MATCHES: the left-hand expression is equal to the correct expression using regex-style comparisons according to ICU v3 (see the ICU User Guide for Regular Expressions for more details).

+1
source
 NSPredicate *pNewsCard = [NSPredicate predicateWithFormat:@"feedType = %@", @"NewsCard"]; NSArray *filteredNewsCard = [arrayTotalFeed filteredArrayUsingPredicate:pNewsCard]; 

feedType is a property of a custom object for comparison, and arrayTotalFeed is an array of custom objects.

0
source

All Articles