Reputation: 10129
I have an object called Contact. A Contact has a relationship with a set of PhoneNumber objects.
A phone number has a label and a value property (both NSString pointers).
How do I get a set of all the phone number labels for a given Contact?
Here is the code for what I want to do, but I think there must be an easier way:
NSSet *phoneNumbersSet = contact.phoneNumbers;
NSArray *phoneNumbersArray = [phoneNumbersSet allObjects];
NSMutableSet *phoneNumberLabelSet = [NSMutableSet setWithCapacity:0];
for (PhoneNumber* phoneNumber in phoneNumbersArray) {
[phoneNumberLabelSet addObject:phoneNumber.label];
}
Upvotes: 0
Views: 1423
Reputation: 641
NSSet *phoneNumbersSet = contact.phoneNumbers;
NSSet *phoneNumberLabelSet = [phoneNumbersSet valueForKey:@"label"];
NSSet has a valueForKey: instance method that calls valueForKey: on each of it's members. I believe that if your set is filled with phoneNumbers you can call valueForKey:@"label" and it will return a set with each of the phoneNumber's respective labels.
Upvotes: 3