I've a person object which has NSString
properties firstname
, lastname
, birthday
, and NSMutableDictionary
of different phone numbers of that person.
I've added different person objects in an NSMutableArray
named personArray
.
I've a person object which has NSString
properties firstname
, lastname
, birthday
, and NSMutableDictionary
of different phone numbers of that person.
I've added different person objects in an NSMutableArray
named personArray
.
Try using this method on NSArray
. Something like this:
return [personArray[[personArray indexOfObjectPassingTest:^ (id obj, NSUInteger index, BOOL stop) {
return [lastName isEqualToString:[obj lastName]];
}]] phoneNumber];
You get the idea.
You should be able to do it like this:
-(NSArray *) phoneNumberFor:(NSString *)lastName{
NSInteger indx = [self.personArray indexOfObjectPassingTest:^BOOL(Person *aPerson, NSUInteger idx, BOOL *stop) {
return [aPerson.lastname isEqualToString:lastName];
}];
if (indx != NSNotFound) {
return [self.personArray[indx] phoneNumbers].allValues;
}else{
return nil;
}
}
In this example, I'm assuming an array property called personArray, a class name of "Person" for your person objects, and a mutable dictionary called phoneNumbers. I'm also assuming that the dictionary contains several keys and values, where all of the values are phone numbers.
You can get object from array of the given lastname like as follow:-
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lastname == '%@'", lastname];
NSArray *foundPersonArray = [personArray filteredArrayUsingPredicate:predicate];
NSLog("found person object = %@",foundPersonArray);
But if you want to search multiple object of same lastname from array then you can do following:-
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lastname LIKE[c] %@", lastname];
NSArray *foundPersonArray = [personArray filteredArrayUsingPredicate:predicate];
NSLog("found person object = %@",foundPersonArray);