从NSArray获取NSArray的单个属性 [英] Getting an NSArray of a single attribute from an NSArray
问题描述
我正面临一个非常常规的情况。
I am facing a very regular scenario.
我有一个NSArray,它有一个自定义类型的对象,比如Person。 Person类具有以下属性:firstName,lastName和age。
I have an NSArray which has object of a custom type, say Person. The Person class has the attributes: firstName, lastName and age.
如何从NSArray中获取一个只包含Person对象的NSArray?
How can I get an NSArray containing only one attribute from the NSArray having Person objects?
类似于:
NSArray *people;
NSArray *firstNames = [people getArrayOfAttribute:@"firstName" andType:Person.Class]
我有一个编写for循环的解决方案并填写firstNames数组但我不想这样做。
I have a solution of writing a for loop and fill in the firstNames array but I don't want to do that.
推荐答案
NSArray将使用KVC为您处理此事
NSArray will handle this for you using KVC
NSArray *people ...;
NSArray *firstName = [people valueForKey:@"firstName"];
这将为您提供数组中每个条目的firstName值数组
This will give you an array of the firstName values from each entry in the array
这篇关于从NSArray获取NSArray的单个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!