Question

I have an NSMutableDictionary that contains key=>obj, these objects are NSMutableArray each one of these NSMutableArray have many objects of Person class:

Person class have id,name,age

NSMutableDictionary => key(A)=>NSMutableArray (person_1,person_2)
NSMutableDictionary => key(H)=>NSMutableArray (person_3,person_5)

How i can find an object of person inside the NSMutableDictionary that has an id of 6 or name of "Tony" using Predicate filter !!

Was it helpful?

Solution

Try this,

 NSArray *persons = [dictionary allValues];
 NSArray *allPersons = [persons valueForKeyPath:@"@unionOfArrays.self"];
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(id == %@) OR (name CONTAINS[cd] %@)", persionId, personName];

 NSArray * resultArray = [allPersons filteredArrayUsingPredicate:predicate];

OTHER TIPS

Assumption about your structure.

{
    "A": [
        "aaa_Persion...",//It indicate Person Object which contain id, name.
        "abc_Person...."
    ],
    "H": [
        "h_Person..........."
    ]
}

If your structure is same as above, Following code will help you..

for(NSArray *array in yourIndexDictinary)
    {
      NSArray *searchedPerson =  [NSPredicate predicateWithFormat:@"(id == %@) || (name CONTAINS[cd] %@)",searchId,searchString];
    }

Note: [cd] means, String comparisons are by default case and diacritic sensitive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top