Question

I have this array with a bunch of NSPropertyDescription objects. These objects have a property called name.

I want to extract an array containing just the names of all these objects.

Ok, I can do this:

NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSPropertyDescription *property in anEntity) {
    [array addObject:property.name];
}

but I know objective-c has a lot of magical commands to extract stuff from arrays of objects.

How do I do that using one of those magical commands? thanks.

Was it helpful?

Solution

Key-Value Coding should do the trick:

NSArray *names = [arrayOfPropertyDescriptions valueForKey:@"name"];

For an array, valueForKey returns an array containing the results of invoking valueForKey: using the key on each of the array's objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top