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.

1
  • What is the question? Commented Oct 5, 2013 at 2:54

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@user2769614: Try putting that code in your phoneNUmberForLastName method. It should work.
that throws error 'NSInvalidArgumentException', reason: '-[Person phoneNumber]: unrecognized selector sent to instance
phone numbers are in NSMutableDictionary inside person object
@user2769614: I can't help you with that unless I can see the Person class.
these are the variables in person object { NSString *firstName; NSString *lastName; NSString *birthday; NSMutableDictionary *phoneNumbers; }
|
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.