从NSArray获取NSArray的单个属性 [英] Getting an NSArray of a single attribute from an NSArray

查看:189
本文介绍了从NSArray获取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屋!

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