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