从嵌套在 NSArray 中的 NSDictionary 中获取特定数据字段
我有一个包含嵌套字典数据的数组,如下所示:
[{ link : [
{ $ref: "foo", $href: "first_foo"},
{ $ref: "bar", $href: "barrrrrrr"},
{ $ref: "quz", $href: "quzzzzzzz"}]},
{ link : [
{ $ref: "foo", $href: "second_foo"},
{ $ref: "bar", $href: "barrrrrrr"},
{ $ref: "quz", $href: "quzzzzzzz"}]}]
我想选出每个字典的 foo
。想要的结果是这样的:
[{ foo: "first_foo"}, { foo: "second_foo"}]
我尝试使用 [myArray valueForKey @"@customFilterForFoo"]
,并为 NSDictionary 上的类别实现了 customFilterForFoo
,但这最终得到 <代码>*** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<__NSCFArray 0x6b81a10>” valueForUndefinedKey:]: 这个类不符合键 customFilterForFoo 的键值编码。'
我该如何做这样的事情?谢谢!
I have a array with nested dictionary data like this:
[{ link : [
{ $ref: "foo", $href: "first_foo"},
{ $ref: "bar", $href: "barrrrrrr"},
{ $ref: "quz", $href: "quzzzzzzz"}]},
{ link : [
{ $ref: "foo", $href: "second_foo"},
{ $ref: "bar", $href: "barrrrrrr"},
{ $ref: "quz", $href: "quzzzzzzz"}]}]
I want to pick out the foo
of each dictionary. The wanting result like this:
[{ foo: "first_foo"}, { foo: "second_foo"}]
I've tried using [myArray valueForKey @"@customFilterForFoo"]
, and implemented the customFilterForFoo
for category on NSDictionary, but this ends up with *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFArray 0x6b81a10> valueForUndefinedKey:]: this class is not key value coding-compliant for the key customFilterForFoo.'
How can i do something like this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在字典数组中循环,然后在每个字典中搜索 foo 的值:
如果您想要“想要的结果”,您可以使用 setValue:forKey: 方法,但由于结果是 Foo 值的数组,因此键值编码不是并非在所有情况下都必需(除非您想在数组中添加其他内容)
You should loop in your array of dictionaries then in each one search for your foo's value :
if you want your "wanting result" you can use the setValue:forKey: method but since the result is an array of Foo's values, key value coding isn't necessary in all cases (unless you want to add something else in your array)