根据 NSArray / NSDictionary 层次结构中的其他对象检索对象
抱歉这个奇怪的标题......一切都会得到解释。
基本上我的应用程序中有一个像这样的数组/字典结构:
NSArray {
NSDictionary {
(NSString *)id;
(NSDictionary *)dictionary;
}
NSDictionary {
(NSString *)id;
(NSDictionary *)dictionary;
}
NSDictionary {
(NSString *)id;
(NSDictionary *)dictionary;
}
etc...
}
希望这很容易解决...
现在我有一个 (NSString *)id
并且我想获取 (NSDictionary *)与之对应的字典
。
有什么办法可以做到这一点吗?
谢谢
汤姆
Sorry about the weird title... all will be explained.
Basically I have an array/dictionary structure like this in my app:
NSArray {
NSDictionary {
(NSString *)id;
(NSDictionary *)dictionary;
}
NSDictionary {
(NSString *)id;
(NSDictionary *)dictionary;
}
NSDictionary {
(NSString *)id;
(NSDictionary *)dictionary;
}
etc...
}
Hope that's easy to work out...
Now I have an (NSString *)id
and I want to get the (NSDictionary *)dictionary
which corresponds to it.
Is there any way that I can do this?
Thank you
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情
我还没有编译代码(我附近没有mac),但我想你明白了。基本思想是循环遍历数组并比较查询字符串。但如果您有很多项目,那么这种线性搜索可能会很耗时。
Try something like this
I have not compiled the code(don't have mac near me), but I think you got the idea. The basic idea is to loop through the array and compare query string. But if you have many items then this linear search might be time consuming.
如果
ids
是唯一的,我会使用NSDictionary
作为我的顶级对象,而不是 NSArray。这样你就可以这样做:[dict objectForKey:stringID]
NSArray
在这里并没有真正的意义,除非你将它用作表数据源。If the
ids
are unique, I'd useNSDictionary
as my top level object instead of the NSArray. That way you can just do:[dict objectForKey:stringID]
NSArray
doesn't really make sense here unless you are using it as a tables datasource.