0

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.

user2769614
  • 247
  • 3
  • 6
  • 23

3 Answers3

1

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.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
0

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.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
0

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);
Sunil Zalavadiya
  • 1,993
  • 25
  • 36