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

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

  •  04-07-2023
  •  | 
  •  

문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top