【问题标题】:Append items to an existing key in NSDictionary saved in a plist将项目附加到保存在 plist 中的 NSDictionary 中的现有键
【发布时间】:2014-03-30 02:23:18
【问题描述】:

如何从 plist 将值附加到 NSDictionary 中的现有键?

我所拥有的基本上是一个保存在磁盘中的 plist,其中包含一些键和值,键是一些具有一个初始值的学生的姓名。我试图做的不起作用是通过读取 plist 将更多项目附加到现有键/学生,将其添加到临时 NSDictionary,将临时数组附加到 plist 中的现有键但是当我保存plist back 它不起作用,它只保存最后两项,并且基本上删除了该键的初始值。

这是我正在使用的代码……

- (IBAction)testing:(id)sender
{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [dirPaths objectAtIndex:0];
    NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"studentsRecords.plist"];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];

    if(fileExists)
    {
        NSMutableDictionary *dictionaryRecords = [[NSMutableDictionary alloc]initWithContentsOfFile:fullPath];
        NSLog(@"Items for Nathan: %@",[dictionaryRecords objectForKey:@"Nathan"]);// here the output is, Items for Nathan: Ruler
        // which make sense since I only had one initial record for Nathan in my plist,

        // Here, I want to add two more items to the key Nathan by appending an arry to
        // NSMutableDictionary (dictionaryRecords) but it doesnt work
        NSArray *tempArray = @[@"NoteBook", @"Pencil"];
        [dictionaryRecords setObject:tempArray forKey:@"Nathan"];

        [dictionaryRecords writeToFile:fullPath atomically:YES];
        // when I check the plist after saving this, I only see the last two values, NoteBook and Pencil
        // instead of the three that I'm expecting, Ruler, NoteBook and Pencil
    }

}

我在这里缺少什么?

非常感谢。

【问题讨论】:

    标签: ios objective-c nsdictionary plist


    【解决方案1】:

    [dictionaryRecords setObject:tempArray forKey:@"Nathan"]tempArray 替换之前的值。

    如果要添加到现有数组,则必须检索它,制作可变副本,然后附加到它。

    【讨论】:

    【解决方案2】:

    这是我如何使它工作的。我希望我在这里没有把事情搞得太复杂,但由于某种原因,我不得不使用两个数组,一个 NSArray 和一个 NSMutableArray,我认为 NSMutableArray 就足够了,但它不起作用。

    - (IBAction)testing:(id)sender
    {
        // get directory path
        NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [dirPaths objectAtIndex:0];
        // create plist
        NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"studentsRecords.plist"];
    
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];
    
        // if file exists create dictionary
        if(fileExists)
        {
            // add items from plist to dictionary
            NSDictionary *dictionaryExistingRecords = [[NSDictionary alloc]initWithContentsOfFile:fullPath];
            // make a copy if dictinary with existing items
            NSMutableDictionary *dictionaryNewRecords = [dictionaryExistingRecords mutableCopy];
            // array to hold existing values from a spcecified key
            NSArray *arrayWithExistingKey = [dictionaryExistingRecords objectForKey:@"Nathan"];
            // mutable array to add existing and be able to insert new items
            NSMutableArray *arrayOldAndNewItems = [NSMutableArray arrayWithArray:arrayWithExistingKey];
            // insert new items to array
            [arrayOldAndNewItems addObject:@"Snow Pans"];
            // add old and new items to specified key
            [dictionaryNewRecords setObject:arrayOldAndNewItems forKey:@"Nathan"];
            // save new records to plist
            [dictionaryNewRecords writeToFile:fullPath atomically:YES];
        }
    }
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      相关资源
      最近更新 更多