是否可以在键下设置NSMutableDictionary键?

时间:2013-04-29 03:34:25

标签: objective-c key nsmutabledictionary

我设置了value key,如此:

[mutableDict setObject:@"Object to set" forKey:@"KeyOne"];

这有效,但现在我想在value下添加"Key one",如下所示:

[mutableDict setObject:@"Object a" forKey:@"KeyOne/detailsOnKey"];

这甚至可能吗?感谢所有帮助

2 个答案:

答案 0 :(得分:1)

听起来你想要字典中的字典dawg

mutableDict[@"KeyOne"] = @{@"detailsOnKey": @"Object"}

然后你可以像这样访问那个嵌套的dict:

mutableDict[@"KeyOne"][@"detailsOnKey"]

答案 1 :(得分:0)

您可以使用多个词典。

//add a dictionary as the object for KeyOne
[mutableDict setObject:[[NSMutableDictionary alloc] init] forKey:@"KeyOne"];

//add "Object a" under key one
[(NSMutableDictionary *)[mutableDict objectForKey:@"KeyOne"] setObject:@"Object a" forKey:@"detailsOnKey"];

//如何获取keyone / detailsonkey?这样你可以把它取回来

id objectA = [(NSMutableDictionary *)[mutableDict objectForKey:@"KeyOne"] objectForKey:@"detailsOnKey"];