从字典数组中,创建包含一个键的值的数组

发布于 2024-08-30 08:11:04 字数 61 浏览 2 评论 0 原文

我有一系列字典。我想提取一个数组,其中包含原始数组中字典的一个特定键的所有元素。不用枚举就可以做到这一点吗?

I have an array of dictionaries. I would like to extract an array with all the elements of one particular key of the dictionaries in the original array. Can this be done without enumeration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爺獨霸怡葒院 2024-09-06 08:11:05

是的,使用 NSArray -valueForKey: 方法。

NSArray *extracted = [sourceArray valueForKey:@"a key"];

Yes, use the NSArray -valueForKey: method.

NSArray *extracted = [sourceArray valueForKey:@"a key"];
墟烟 2024-09-06 08:11:05

是的,只需使用键值编码来询问键的值:

NSArray* names = [NSArray arrayWithObjects:
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Joe",@"firstname",
                   @"Bloggs",@"surname",
                   nil],
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Simon",@"firstname",
                   @"Templar",@"surname",
                   nil],
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Amelia",@"firstname",
                   @"Pond",@"surname",
                   nil],
                  nil];

//use KVC to get the names
NSArray* firstNames = [names valueForKey:@"firstname"];

NSLog(@"first names: %@",firstNames);

Yes, just use Key-Value Coding to ask for the values of the key:

NSArray* names = [NSArray arrayWithObjects:
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Joe",@"firstname",
                   @"Bloggs",@"surname",
                   nil],
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Simon",@"firstname",
                   @"Templar",@"surname",
                   nil],
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Amelia",@"firstname",
                   @"Pond",@"surname",
                   nil],
                  nil];

//use KVC to get the names
NSArray* firstNames = [names valueForKey:@"firstname"];

NSLog(@"first names: %@",firstNames);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文