iPhone - 更改子子子子 NSMutableDictionary 值

发布于 2024-11-06 00:17:40 字数 784 浏览 2 评论 0原文

如果我有一个类似于以下内容的数据树:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短叹 2024-11-13 00:17:40

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

但是,人们假设您拥有的这种数据结构代表某种数据模型。因此,它可能应该包含在某种理解其各个部分的模型类中,并且该类应该是唯一需要理解数据如何存储的类。模型类应该为数据提供更高级别的接口。假设 dict3 代表车队中的一辆特定车辆,键是“vehicleTag”、“registrationDate”、“purchaseDate”等。可能 dict2 级别的字典代表不同地区的车队,而 dict2 本身代表东北车队。然后,VehiclePool 类(存储所有数据的模型类)可能会提供以下方法:

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

我不确定我是否真的需要这样的 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:

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

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.

铁轨上的流浪者 2024-11-13 00:17:40

NSDictionary< /code>文档指出:

一般来说,键可以是任何对象(只要它符合 NSCopying 协议——见下文),但请注意,使用键值编码时,键必须是字符串(参见“键值编码基础知识”) )。

因此,如果您的键(和子键)都是字符串,您可以执行以下操作来检索和设置值:

id myNestedObject = [topLevel valueForKeyPath:@"firstKey.secondKey.thirdKey"];
[topLevel setObject:newNestedObject forKeyPath:@"firstKey.secondKey.thirdKey"];

The NSDictionary documentation states:

In general, a key can be any object (provided that it conforms to the NSCopying protocol—see below), but note that when using key-value coding the key must be a string (see “Key-Value Coding Fundamentals”).

Therefore, if your keys (and sub-keys) are all strings, you can do the following to retrieve and set values:

id myNestedObject = [topLevel valueForKeyPath:@"firstKey.secondKey.thirdKey"];
[topLevel setObject:newNestedObject forKeyPath:@"firstKey.secondKey.thirdKey"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文