2014-03-30 27 views
2

如何將值附加到來自plist的NSDictionary中的現有密鑰?將項目附加到保存在plist中的NSDictionary中的現有密鑰

我所擁有的基本上是保存在具有幾個鍵和值的光盤中的plist,這些鍵是具有一個初始值的一些學生的名字。我試圖做的是不工作的是通過閱讀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 
    } 

} 

缺少什麼我在這裏?

非常感謝。

回答

5

[dictionaryRecords setObject:tempArray forKey:@"Nathan"]取代以前的值是tempArray

如果你想添加到現有的數組,你必須檢索它,做一個可變的副本,並追加到它。

+0

找回來自plist?請原諒我的無知,但我該怎麼做?對不起,但正如你可能已經知道我沒有很多與plists和NSDictionaries的經驗。 –

+1

你的代碼已經在你的問題中做了。 'dictionaryRecords'是plist的內容。 –

+0

我知道我知道,但我怎麼能做一個可變的副本?謝謝 –

0

這是我如何使它工作。我希望我沒有在這裏複雜的東西,但由於某種原因,我不得不使用兩個數組,一個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 
+0

其不追加! –

相關問題