iPhone-更改sub-sub-sub NSMutableDictionary值 [英] iPhone - Changing a sub-sub-sub NSMutableDictionary value

查看:63
本文介绍了iPhone-更改sub-sub-sub NSMutableDictionary值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个像这样的数据树:

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

如果我想更改Key1的值,是否有比...更简单的方法

If I want to change the value of Key1, is there a simplier way than...

获取dict1
然后得到dict2
然后得到数组
然后得到dict3

getting dict1
then getting dict2
then getting array
then getting dict3

将dict3转换为可变字典
然后设置key1

converting dict3 to a mutable Dictionary
then setting key1

将数组转换为可变数组
然后将dict3设置为数组

Converting array to a mutable array
then setting dict3 into array

将dict2转换为可变字典
然后将数组设置为dict2

converting dict2 to a mutable Dictionary
then setting array into dict2

将dict1转换为可变字典
然后将dict2设置为dict1

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.

推荐答案

除非您有指向该对象的指针,否则您无法将消息发送给该对象,因此从基本意义上讲,问题的答案是否",没有其他方法.

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.

但是,有人认为您拥有的这种数据结构代表某种数据模型.因此,它可能应该包含在某种模型类中,该类可以理解其组成部分,并且该类应该是唯一需要了解数据存储方式的模型类.模型类应为数据提供更高级别的接口.假设dict3代表车队中的一种特定车辆,并且键是诸如"vehicleTag","registrationDate","purchaseDate"之类的东西.也许dict2的词典将不同区域中的车队设为水平,而dict2本身代表东北车队.然后,VehiclePool类(您的模型类,用于存储所有数据)可能会提供以下方法:

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:

-registrationDateForVehicle:(int)vehicleIndex inFleet:(NSString*)fleetKey;
-setRegistrationDate:(NSDate*)regDate forVehicle:(int)vehicleIndex inFleet:(NSString*)fleetKey;

我不确定我是否真的想要像这样的API-我更喜欢获取给定车队的车辆清单,然后在具有更简单访问器的车辆上进行操作,但是您似乎想要避免几个访问者级别.这里的要点是,您不应编写大量代码来执行所需的操作;您应该编写知道如何访问数据的方法,然后调用这些方法.

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.

这篇关于iPhone-更改sub-sub-sub NSMutableDictionary值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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