2014-03-30 3 views
2

가 어떻게이 PLIST에서 NSDictionary와의 기존 키에 값을 추가 할 수있는 PLIST에 저장? 내가 가진 무엇추가] 항목

는 기본적으로 몇 개의 키와 값을 사용하여 디스크에 저장된 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 
    } 

} 

입니까?

미리 감사드립니다. 이전 값이 tempArray로했다 어떤

답변

5

[dictionaryRecords setObject:tempArray forKey:@"Nathan"] 대체합니다.

기존 배열에 추가 할 경우

, 당신은 그것을 검색 변경 가능한 사본을 만들고, 여기에 추가해야합니다.

+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

해당 추가되지 않습니다! –