获取 NSArray 内 NSDictionary 中的所有值
我有一个充满 NSDictionary 对象的 NSArray,每个 NSDictionary 内部都有一个唯一的 ID。我想根据 ID 查找特定词典,并在我自己的词典中获取该词典的所有信息。
myArray 包含:
[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...)
我想获取第二个字典对象的名称和权重(我不知道该对象的索引...如果只有两个字典,我可以从 [myArray objectAtIndex 制作一个字典:1]
)
那么,假设我知道数字 149。我如何才能将 myArray 中的第二个 myDictionary 对象放入新的 NSDictionary 中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为雅各布答案的替代方案,您也可以直接要求字典查找该对象:
As an alternative to Jacob's answer, you could also just ask the dictionary to find the object:
您需要遍历
NSArray
中的每个NSDictionary
对象:You'd need to iterate through every
NSDictionary
object in yourNSArray
: