Magic command for extracting an array of names from an array of NSPropertyDescription objects

StackOverflow https://stackoverflow.com/questions/23093865

  •  04-07-2023
  •  | 
  •  

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.

Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top