从对象数组获取属性的数组 [英] Getting an array of a property from an object array
本文介绍了从对象数组获取属性的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从自定义对象数组中提取单个属性的数组。例如。
I need to extract an array of a single property from a custom object array. eg.
@interface MyClass : NSObject
{
int sampleNumber;
NSString *sampleName;
}
我有一个数组 MyClass
实例 myArray
。我想要一个 sampleName
字符串的数组。有没有办法做到这一点,而无需遍历整个数组像这样:
I have an array of MyClass
instances called myArray
. I want to then get an array of the sampleName
strings. Is there a way to do it without stepping through the whole array like this:
NSMutableArray *stringArray;
for (MyClass *thisInstance in myArray)
{
[stringArray addObject:thisInstance.sampleName];
}
我试图在Objective-C中搜索一个类似的问题,在PHP和LINQ部分。
I attempted to search for a similar question in Objective-C but found it only in the PHP and LINQ sections.
推荐答案
使用键值编码:
NSArray *stringArray = [myArray valueForKey:@"sampleName"];
这篇关于从对象数组获取属性的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文