In #42: #valueForKey on NSArrays, I described how you can use -valueForKey:
on NSArray
instances as a quick way to collect the property of every element in an array. There's more. If you look at the doc for NSKeyValueCoding
protocol, you'll find this gem -valueForKeyPath:
. You can do stuff like:
NSArray* managerLastNames = [departments valueForKeyPath:@"manager.lastName"];
and it will retrieve every department's manager's last name.
If you have an employees property that is an array, this will retrieve every employees last names, with the array structure preserved.
NSArray* employeeLastNames = [departments valueForKeyPath:@"employees.lastName"];
This works even if you have multiple layers of array properties.
Your feedback is valuable: Do you want more nuggets like this? Yes or No
.