-1

I need to write a method that searches for a phone number inside personArray and should return the name associated with that phone number.

- (NSString *) nameForNumber:(NSString *)phoneNumber{

}
user2769614
  • 247
  • 3
  • 6
  • 23

1 Answers1

1

What you should do, is to learn how to use the NSArray method indexOfObjectPassingTest:. This is a very useful method for finding things in arrays. You current question can be solved like this:

- (NSString *) nameForNumber:(NSString *)phoneNumber{
    NSInteger indx = [self.personArray indexOfObjectPassingTest:^BOOL(Person *aPerson, NSUInteger idx, BOOL *stop) {
        return [aPerson.phoneNumbers.allValues containsObject:phoneNumber];
    }];
    if (indx != NSNotFound) {
        return [self.personArray[indx] lastname];
    }else{
        return @"Not Found";
    }
}

The names I'm using are based on the same assumptions that I made in my answer to one of your other questions (here).

Community
  • 1
  • 1
rdelmar
  • 103,982
  • 12
  • 207
  • 218