-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{

}
0

1 Answer 1

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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.