iPhone - 更改子子子子 NSMutableDictionary 值
如果我有一个类似于以下内容的数据树:
NSMutableDictionary (dict1)
NSMutableDictionary (dict2)
NSMutableArray (array)
NSMutableDictionary (dict3)
Key1
Key2
NSMutableDictionary (dict_n)
Keyn
Keyn
NSMutableDictionary (dict_n)
NSMutableArray (array_n)
NSMutableDictionary (dict_n)
Keyn
Keyn
NSMutableDictionary (dict_n)
Keyn
Keyn
如果我想更改 Key1 的值,有没有比...
获取 dict1 更简单的方法
然后得到 dict2
然后获取数组
然后获取 dict3
将 dict3 转换为可变字典
然后设置 key1
将数组转换为可变数组
然后将 dict3 设置为数组,
将 dict2 转换为可变字典
然后将数组设置为 dict2
将 dict1 转换为可变字典
然后将 dict2 设置为 dict1
对于我必须更改的每个值执行此操作确实令人头疼,并且非常耗时。
If I have a data tree that is something like :
NSMutableDictionary (dict1)
NSMutableDictionary (dict2)
NSMutableArray (array)
NSMutableDictionary (dict3)
Key1
Key2
NSMutableDictionary (dict_n)
Keyn
Keyn
NSMutableDictionary (dict_n)
NSMutableArray (array_n)
NSMutableDictionary (dict_n)
Keyn
Keyn
NSMutableDictionary (dict_n)
Keyn
Keyn
If I want to change the value of Key1, is there a simplier way than...
getting dict1
then getting dict2
then getting array
then getting dict3
converting dict3 to a mutable Dictionary
then setting key1
Converting array to a mutable array
then setting dict3 into array
converting dict2 to a mutable Dictionary
then setting array into dict2
converting dict1 to a mutable Dictionary
then setting dict2 into dict1
Doing that for each value I have to change is a real headache, and really code consuming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非有指向该对象的指针,否则您无法向该对象发送消息,因此从基本意义上讲,您的问题的答案是否定的,没有其他方法。
但是,人们假设您拥有的这种数据结构代表某种数据模型。因此,它可能应该包含在某种理解其各个部分的模型类中,并且该类应该是唯一需要理解数据如何存储的类。模型类应该为数据提供更高级别的接口。假设 dict3 代表车队中的一辆特定车辆,键是“vehicleTag”、“registrationDate”、“purchaseDate”等。可能 dict2 级别的字典代表不同地区的车队,而 dict2 本身代表东北车队。然后,VehiclePool 类(存储所有数据的模型类)可能会提供以下方法:
我不确定我是否真的需要这样的 API — 我更愿意获取给定车队的车辆列表,并且然后对那些具有更简单访问器的操作进行操作,但您似乎想避免多个级别的访问器。这里的要点是,您不应该编写大量代码来执行您需要的操作;您应该编写知道如何访问数据的方法,然后调用它们。
You can't send a message to an object unless you have a pointer to that object, so in a basic sense the answer to your question is no, there's no other way.
However, one presumes that this data structure that you have represents some sort of data model. As such, it should probably be contained in some sort of model class that understands its parts, and that class should be the only one that needs to understand how the data is stored. The model class should offer a higher-level interface to the data. Say dict3 represents one particular vehicle in a fleet, and the keys are things like "vehicleTag", "registrationDate", "purchaseDate", etc. Maybe the dictionaries at the dict2 level the fleets in different regions, and dict2 itself represents the northeast fleet. Then the VehiclePool class, your model class which stores all the data, might offer methods like:
I'm not sure I'd really want an API like that -- I'd prefer to get the list of vehicles for a given fleet and then operate on those with simpler accessors, but you seem to want to avoid several levels of accessors. The point here is that you shouldn't be writing a ton of code to do the operations you need; you should write methods that know how to access the data and then call those.
NSDictionary< /code>
文档指出:
因此,如果您的键(和子键)都是字符串,您可以执行以下操作来检索和设置值:
The
NSDictionary
documentation states:Therefore, if your keys (and sub-keys) are all strings, you can do the following to retrieve and set values: