从对象数组中获取属性数组 [英] Getting an array of a property from an object array

查看:42
本文介绍了从对象数组中获取属性数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从自定义对象数组中提取单个属性的数组.例如.

I need to extract an array of a single property from a custom object array. eg.

@interface MyClass : NSObject 
{
    int sampleNumber;
    NSString *sampleName;
}

我有一个名为 myArrayMyClass 实例数组.然后我想获得 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆