【问题标题】:With a plist containing an array of arrays of dictionaries, how do you add and write out new dictionary elements使用包含字典数组的 plist,如何添加和写出新的字典元素
【发布时间】:2011-06-25 03:25:38
【问题描述】:

我创建了一个 plist,它被初始化为字典数组的数组。 plist 存储动态数据。因此,当应用程序终止时,可能会添加新的字典元素。或者可能需要添加新的字典元素数组。稍后,可能会有更少的字典元素,从而不再需要以前的元素。虽然有很多关于向 plist 添加元素的讨论,但似乎没有一个能解决我的问题。这是最初的 plist 描述:

Key                  Type         Value
Root                 Dictionary   (2 items)
  Hourly Data        Array        (1 item)
     Item 0          Array        (1 item)      <--how do I add more of these
        Item 0       Dictionary   (3 items)     <--how do I add more of these
           Name      String       Joe
           Rank      String       private
           serialNo  String       123456
  NonHourly Data     Array        (1 item)
     Item 0          Array        (1 item)
        Item 0       Dictionary   (3 items)
           Name      String       Jeff
           Rank      String       private
           serialNo  String       654321

虽然我完全了​​解如何读取此 plist 文件,但我不明白如何将数组元素添加到 Hourly 和 NonHourly Data 数组,或者如何添加新的 Dictionary 数组项,然后将它们写回 plist .

那么以下面的代码为起点,我该如何完成这段代码来添加上述的数组元素和字典元素:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);   // Create a list of paths
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *configPlist = [documentsDirectory stringByAppendingPathComponent:@"config.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: configPlist];

...

[data writeToFile: configPlist atomically:YES];

【问题讨论】:

    标签: objective-c ios plist


    【解决方案1】:

    好的,在尝试了一个错误之后,我想出了如何做到这一点。首先,让我为正在存储的数据提供一个定义。该数据适用于所有具有每小时和非每小时状态的军人。 “每小时数据”和“非每小时数据”被视为 1 级数组。 1 级数组的元素称为 2 级数组。二级阵可以认为是军队人员曾服役的国家。二级阵有字典的元素。每个字典都描述了一个军队。

    现在要找到答案,基本上我需要从内到外创建一个包含要存储的数据的 2 级字典数组。然后将该级别 2 数组添加到级别 1 数组中。最后,1 级数组作为对象存储在 Plist 的 Hourly Data 或 Non-Hourly Data 键中。这是解决方案:

    // Create an array of arrays of content encoded as dictionary objects for hourly data
    armyPerson *aPerson;
    NSArray *level1Element;
    NSMutableArray *level2Array;    
    NSMutableArray *level1Array = [[[NSMutableArray alloc] initWithObjects:nil] autorelease];
    for (int i=0; i<[allHourlyPersons count]; i++) {
        level1Element = [allHourlyPersons objectAtIndex:i]; // grab the next level1 array element. 
    
        // Each level1 array element will have zero or more level2Arrays. The info for each level2 dictionary needs to be collected into
        // an level2Array element as individual dictionary objects
        level2Array = [[[NSMutableArray alloc] initWithObjects:nil] autorelease];   // initialize the level2Array
        for (int j=0; j<[level1Element count]; j++) {
            aPerson = [level1Element objectAtIndex:j];  // grab the next armyPerson
    
            // For each episode, create a dictionary object
            NSDictionary *level2Element = [NSDictionary dictionaryWithObjectsAndKeys:aPerson.name, @"Name", 
                                                                                        aPerson.rank, @"Rank",
                                                                                        aPerson.serialNo, @"serialNo", nil];
            [level2Array addObject:level2Element];  // save the info for this episode
        }
    
        // Now that we have all the episodes for this level1Element collected into an array of dictionary objects,
        // we need to add this array to the level1 array
        [level1Array addObject:level2Array];
    }
    
    // Finally, we need to create the key-value pair for the hourly source array
    [data setObject:level1Array forKey:@"Hourly Data"];
    
    ...
    Do the same for the Non-Hourly Data prior to:
    
    [data writeToFile: configPlist atomically:Yes];
    

    【讨论】:

      猜你喜欢
      • 2019-03-29
      • 2014-12-16
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多