简体   繁体   English

NSMutableDictionary setObject forKey forKey…等

[英]NSMutableDictionary setObject forKey forKey…etc

I'm trying to setObjects that are deep in my NSMutableDictionary, by that I mean there are a few levels of keys before I can get to them. 我正在尝试设置NSMutableDictionary中深层的setObjects,这意味着在我可以访问它们之前,有一些级别的键。

Obviously I can't do 显然我做不到

setObject:x forKey forKey forKey forKey...

How can I achieve that? 我该如何实现?

You would just do something like 你会做类似的事情

mutableDictionary[key1][key2][key3] = value;

BUT this multi nesting of dictionaries is most likely hinting at a bigger problem with your design. 但是字典的这种多重嵌套最有可能暗示您的设计存在更大的问题。

Use key-path coding. 使用密钥路径编码。 Something like: 就像是:

[myDictionary setValue:newValue forKeyPath:@"foo.bar.baz"];

That looks up foo on myDictionary , then takes whatever it gets and looks up bar on that, then takes whatever it gets that time and calls setValue:forKey: on that to assign a new value for baz . 那会在myDictionary上查找foo ,然后获取它所获取的一切并在其上查找bar ,然后花费那个时间所获取的一切,并在其上调用setValue:forKey:baz分配一个新值。 You can also use key paths to look up values: 您还可以使用键路径来查找值:

id currentValue = [myDictionary valueForKeyPath:@"foo.bar.baz"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM