xcode 将一个对象插入到 NSDictionary 的 NSMutablearray 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6947621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert an object to an NSMutablearray of NSDictionary
提问by TommyG
I am trying to insert an object to an array of NSDictionary but cant find out how. all the examples show how to insert to an array with NSDictionary with insertObject:atIndex, but this one doesnt insert the object under a dictionary key.
我正在尝试将一个对象插入到 NSDictionary 数组中,但无法找到方法。所有的例子都展示了如何使用带有 insertObject:atIndex 的 NSDictionary 插入到一个数组中,但是这个没有在字典键下插入对象。
My array right now is the following:
我现在的数组如下:
NSArray *aaa = [NSArray arrayWithObjects:@"Disable" ,nil];
NSDictionary *aaaDict = [NSDictionary dictionaryWithObject:aaa forKey:@"Computers"];
NSArray *bbb = [NSArray arrayWithObjects:@"Enable", nil];
NSDictionary *bbbDict = [NSDictionary dictionaryWithObject:bbb forKey:@"Computers"];
NSArray *ccc = [NSArray arrayWithObjects:@"Enable", nil];
NSDictionary *cccDict = [NSDictionary dictionaryWithObject:ccc forKey:@"Computers"];
[listOfItems addObject:aaaDict];
[listOfItems addObject:bbbDict];
[listOfItems addObject:cccDict];
I would like to do this, but with a key "Computers":
我想这样做,但有一个关键的“计算机”:
NSArray *ar = [NSArray arrayWithObjects:@"xxxx", @"yyyy", @"zzzz", nil];
NSDictionary *arDict = [NSDictionary dictionaryWithObject:ar forKey:@"Computers"];
[listOfItems insertObject:ar atIndex:1];
Again - it does add the new array but not under the key "Computers". What am I missing here?
再次 - 它确实添加了新数组,但不在键“计算机”下。我在这里错过了什么?
Thanks!
谢谢!
回答by Jacob Gorban
You probably wanted to write[listOfItems insertObject:arDict atIndex:1];
instead of[listOfItems insertObject:ar atIndex:1];
你可能想写[listOfItems insertObject:arDict atIndex:1];
而不是[listOfItems insertObject:ar atIndex:1];
You're adding the array and not the dictionary like in the first part of your question.
您正在添加数组,而不是像问题的第一部分那样添加字典。
回答by sbooth
I'm not sure I understand exactly what you're trying to do, but did you mean to insert the ar object or the arDict object? I think your code should look like
我不确定我是否完全理解您要做什么,但是您的意思是插入 ar 对象还是 arDict 对象?我认为你的代码应该看起来像
NSArray *ar = [NSArray arrayWithObjects:@"xxxx", @"yyyy", @"zzzz", nil];
NSDictionary *arDict = [NSDictionary dictionaryWithObject:ar forKey:@"Computers"];
[listOfItems insertObject:arDict atIndex:1];
回答by alexcash
Do you mean you want to add another array to a dictionary inside your listOfItems array? If that is the case it requires a nested method call as such:
您的意思是要将另一个数组添加到 listOfItems 数组中的字典中吗?如果是这种情况,它需要一个嵌套的方法调用,如下所示:
[[listOfItems objectAtIndex:0] setObject:ar forKey:@"Computers"];
回答by Praveen-K
With leak fixed issue
泄漏修复问题
NSArray *ar = [NSArray arrayWithObjects:@"xxxx", @"yyyy", @"zzzz", nil];
NSDictionary *arDict = [NSDictionary dictionaryWithObject:ar forKey:@"Computers"];
[listOfItems insertObject:arDict atIndex:1];
[ar release]; // fixed the leak
[arDict release]; // fixed the leak
回答by Praveen S
When it adds the new object you have to get the NSDictionary first. So basically you are using the dictionary wrong. What you have done is equivalent to
当它添加新对象时,您必须首先获得 NSDictionary。所以基本上你用错了字典。你所做的相当于
[listOfItems addObject:aaa];and similarly bbb and ccc and ar
[listOfItems addObject:aaa];和类似的 bbb 和 ccc 和 ar
What you actually want to do is add the objects to a Dictionary and not a array.
您真正想要做的是将对象添加到 Dictionary 而不是数组。
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary addObject:[NSArray arrayWithObjects:@"Disable" ,nil] forKey:@"Key1"]; // your keys need to be unique
[dictionary addObject:[NSArray arrayWithObjects:@"Enable", nil] forKey:@"Key2"];
[dictionary addObject:[NSArray arrayWithObjects:@"Enable", nil] forKey:@"Key3"];
[dictionary addObject:[NSArray arrayWithObjects:@"xxxx", @"yyyy", @"zzzz", nil] forKey:@"Key4"];

