在NSArray中获取NSDictionary中的所有值 [英] Grab all values in NSDictionary inside an NSArray

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

问题描述

我有一个充满NSDictionary对象的NSArray,每个NSDictionary里面都有一个唯一的ID。我想根据ID查找特定词典,并在我自己的词典中获取该词典的所有信息。



myArray包含:

  [index 0] myDictionary对象
name = apple,
weight = 1 pound,
number = 294,

[index 1] myDictionary对象
name = pear,
weight = .5 pound,
number = 149,

[index 3 ] myDictionary对象(等等......)

我想得到第二本字典的名称和重量object(我不知道对象的索引...如果只有两个dicts,我可以从 [myArray objectAtIndex:1] 中创建一个字典<) / p>

所以,我知道数字149.我怎样才能将myArray中的第二个myDictionary对象变成新的NSDictionary?

解决方案

作为雅各布答案的替代方案,你也可以让字典找到对象: / p>

  NSPredicate * finder = [NSPredicate predicateWithFormat:@number = 149]; 
NSDictionary * targetDictionary = [[array filteredArrayUsingPredicate:finder] lastObject];


I have an NSArray full of NSDictionary objects, and each NSDictionary has a unique ID inside. I want to do lookups of particular dictionaries based on the ID, and get all the information for that dictionary in my own dictionary.

myArray contains:

[index 0] myDictionary object
  name = apple,
  weight = 1 pound,
  number = 294,

[index 1] myDictionary object
  name = pear,
  weight = .5 pound,
  number = 149,

[index 3] myDictionary object (etc...)

I want to get the name and weight for the second dictionary object (I won't know the index of the object... if there were only two dicts, I could just make a dictionary from [myArray objectAtIndex:1])

So, say I know the number 149. How would I be able to get the second myDictionary object out of myArray into a new NSDictionary?

解决方案

As an alternative to Jacob's answer, you could also just ask the dictionary to find the object:

NSPredicate *finder = [NSPredicate predicateWithFormat:@"number = 149"];
NSDictionary *targetDictionary = [[array filteredArrayUsingPredicate:finder] lastObject];

这篇关于在NSArray中获取NSDictionary中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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