gee*_*guy 5 iphone objective-c nsdictionary nsarray
我有一个NSArray充满了NSDictionary对象,每个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...)
Run Code Online (Sandbox Code Playgroud)
我想得到第二个字典对象的名称和重量(我不知道对象的索引...如果只有两个字母,我可以从中制作一个字典[myArray objectAtIndex:1])
所以,比方说我知道数字149.我怎样才能将myArray中的第二个myDictionary对象变成一个新的NSDictionary?
作为雅各布答案的替代方案,你也可以让字典找到对象:
NSPredicate *finder = [NSPredicate predicateWithFormat:@"number = 149"];
NSDictionary *targetDictionary = [[array filteredArrayUsingPredicate:finder] lastObject];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3422 次 |
| 最近记录: |