如何將值附加到來自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
}
}
缺少什麼我在這裏?
非常感謝。
找回來自plist?請原諒我的無知,但我該怎麼做?對不起,但正如你可能已經知道我沒有很多與plists和NSDictionaries的經驗。 –
你的代碼已經在你的問題中做了。 'dictionaryRecords'是plist的內容。 –
我知道我知道,但我怎麼能做一個可變的副本?謝謝 –