如何从NSArray中的NSDictionaries中获取值 [英] How to get values from NSDictionaries inside an NSArray
问题描述
我想知道是否可以在 NSArray $ c中的
NSDictionary
中获取所需键的值$ c>。
I'd like to know if it's possible to get a values from a desired key inside an NSDictionary
that is in NSArray
.
示例:
NSArray *array = @[ @{@"title" : @"title 1", @"description" : @"description 1"},
@{@"title" : @"title 2", @"description" : @"description 2"},
@{@"title" : @"title 3", @"description" : @"description 3"} ];
我想得到(没有创建新的 NSMutableArray
)我的 NSArray
中的所有标题。也许我做错了什么,我的做法完全不好。
I'd like to get (without creating a new NSMutableArray
) all the titles inside my NSArray
. Maybe I'm doing something wrong and my approach is bad altogether.
我知道我可以创建一个新类并有2个属性(标题,desc),但是在那里一种不创建类或创建新的 NSMutableArray
并迭代我的数组的方法?
I know I could create a new class and have 2 properties (title, desc), but is there a way without creating a class or making a new NSMutableArray
and iterating thorough my array?
推荐答案
简单:
NSArray *titles = [array valueForKey:@"title"];
这是因为 NSArray
的实施 valueForKey:
为每个数组的对象返回一个包含 valueForKey:
结果的新数组。
This works because NSArray
's implementation of valueForKey:
returns a new array containing the results of valueForKey:
for each of the array's objects.
这篇关于如何从NSArray中的NSDictionaries中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!