向嵌套NSDictionary添加密钥 [英] Adding keys to Nested NSDictionary

查看:127
本文介绍了向嵌套NSDictionary添加密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为嵌套NSDictionary创建新密钥时遇到问题.这就是我所做的

I am having problems creating new keys for a Nested NSDictionary. Here's what I have done

我有这种NSMutableDictionary

I have this kind of NSMutableDictionary

  NSMutableDictionary *Ga=[NSMutableDictionary dictionaryWithDictionary:@{@"Node1" :@{@"SubNode11" :@40,@"SubNode12":@30}}];

哪个NSLogs为:

Node1 =     {
    SubNode11 = 40;
    SubNode12 = 30;
};

现在要添加另一个根键和一个嵌套键,

Now to add another root key and a nested key I did this,

[Ga setObject:@{@"SubNode21" : @555} forKey:@"Node2"];

现在,NSLog输出:

Now the NSLog outputs:

Node1 =     {
    SubNode11 = 40;
    SubNode12 = 30;
};
Node2 =     {
    SubNode21 = 555;
};
}

我需要向现有节点添加另一个密钥,例如SubNode22 = 345;在单独的代码行中处理Node2,所以我认为这可能有效

I need to add another key to an existing Node, say SubNode22=345; for Node2 in a separate line of code, so I thought this might work

[[Ga objectForKey:@"Node2"] setObject:@5555 forKey:@"SubNode22"];

但是此显示错误由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSDictionaryI setObject:forKey:]:无法识别的选择器已发送到实例"

But this show Error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance"

我不是问题所在,这种方法对我来说直截了当.请采取任何解决方案.

I don't what the Problem is,, this method seems straight forward to me.. Any solution please.

推荐答案

此处的问题与嵌套无关.它与可变的经节不可变有关.如果字典不可变,则无法添加.

The problem here has nothing to do with nesting. It has to do with mutable verse immutable. If the dictionary isn't mutable you can't add to it.

错误消息告诉您,NSDictionary没有名为setObject:ForKey:的方法,因为这是NSMutableDictionary的方法.使用Apple的新文字字典@{ key: object}仅创建不可变的字典.

The error message is telling you, that NSDictionary's don't have a method called setObject:ForKey: because that is a method of NSMutableDictionary. Using Apples new literal dictionaries @{ key: object} only creates immutable dictionaries.

因此,您真正需要的是确保使用[NSMutableDictionary dictionaryWithObjectsAndKeys:];[@{ Key: Object } mutableCopy]

So what you actually need is to make sure you're created NSMutableDictionarys using either [NSMutableDictionary dictionaryWithObjectsAndKeys:]; or [@{ Key: Object } mutableCopy]

这是您的代码更改了

NSMutableDictionary *subNode = [NSMutableDictionary dictionaryWithObjectsAndKeys:@40, @"SubNode11", @30, @"SubNode12", nil];
NSMutableDictionary *Ga = [NSMutableDictionary dictionaryWithObjectsAndKeys:subNode, @"Node 1", nil];
[Ga setObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@555, @"SubNode21", nil] forKey:@"Node2"];
[[Ga objectForKey:@"Node2"] setObject:@5555 forKey:@"SubNode22"];

尽管如此,您仍然可以使用一些新的下标功能.例如,您可以将代码更改为类似的代码,使它们更易读:

You can still use some of the new subscripting features though. For instance you can change the code to something like this thats more readable doing the same things:

NSMutableDictionary *Ga = [NSMutableDictionary dictionary];

// Create SubNodes
NSMutableDictionary *subNode1 = [NSMutableDictionary dictionary];
subNode1[@"SubNode11"] = @40;
subNode1[@"SubNode12"] = @30;

NSMutableDictionary *subNode2 = [NSMutableDictionary dictionary];
subNode2[@"SubNode21"] = @555;

// Set SubNodes to Main Node Container
Ga[@"Node1"] = subNode1;
Ga[@"Node2"] = subNode2;

// Set a nested subnode's value.
Ga[@"Node2"][@"SubNode22"] = @5555;

这篇关于向嵌套NSDictionary添加密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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