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中的NSDictionary中的所需键中获取值。
Example:
例:
NSArray *array = @[ @{@"title" : @"title 1", @"description" : @"description 1"},
@{@"title" : @"title 2", @"description" : @"description 2"},
@{@"title" : @"title 3", @"description" : @"description 3"} ];
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.
我想得到(没有创建一个新的NSMutableArray)我的NSArray中的所有标题。也许我做错了什么,我的做法完全不好。
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?
我知道我可以创建一个新类并有2个属性(title,desc),但有没有一种方法可以创建一个类或创建一个新的NSMutableArray并迭代我的数组?
1 个解决方案
#1
28
Easy:
简单:
NSArray *titles = [array valueForKey:@"title"];
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的valueForKey实现:为每个数组的对象返回一个包含valueForKey:的结果的新数组。
#1
28
Easy:
简单:
NSArray *titles = [array valueForKey:@"title"];
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的valueForKey实现:为每个数组的对象返回一个包含valueForKey:的结果的新数组。