将项目追加到保存在plist中的NSDictionary中的现有键



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

我所拥有的基本上是一个保存在磁盘中的列表,其中包含一些键和值,键是一些具有一个初始值的学生的名字。我要做的是不工作是附加更多的项目到现有的键/学生通过读取plist,将其添加到一个临时的NSDictionary,附加一个临时数组到现有的键在plist但当我保存plist回来它不工作,它只保存最后两个项目,基本上删除了初始值为那个键。

这是我正在使用的代码…

- (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
    }
}

我在这里错过了什么?

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

如果您想添加到现有的数组,您必须检索它,创建一个可变副本,并追加到它。

我是这样做的。我希望我没有把事情弄得太复杂但由于某些原因,我不得不使用两个数组,一个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

最新更新