2014-04-04 4 views
0

많은 검색을 한 후에 이전 오류를 수정하는 코드를 만들 수있었습니다. 내가 성취하려고하는 것은 UITextField에서 사용자 입력을 받아들이고 plist로부터 채워지는 TableView에 그 입력을 추가하는 것입니다. 내 plist의 루트 값은 사전이며 나는 그것을 그대로 유지하고 싶습니다. 내 문제는 writeToFile을 호출 할 때 기존 데이터를 삽입, 추가 또는 추가하는 대신 기존 값을 완전히 쓰는 것입니다. 새로운 값만이 plist에 저장되기 때문에 두 개의 사전을 결합하려는 시도가 작동하지 않는 것 같습니다. 내가 어디로 잘못 가고 있는지에 대한 통찰력? 다음 코드는 내가 당신이 키 Room의 값을 덮어 추측문서 폴더에 plist에 새 입력 추가

//////////********** Add New Cell When OK is Chosen 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

UITextField *newDevice = [alertView textFieldAtIndex:0]; 
NSLog(@"newDevice - %@",newDevice.text); 

    if (alertView.tag == 1 && buttonIndex == 1){ 

NSMutableDictionary *input = [[NSMutableDictionary alloc]init]; 
[input setObject:[NSArray arrayWithObject:newDevice.text] forKey:@"Room"]; 


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Test.plist"]; 
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 

// Combine the two Dictionaries to create one 
[dictionary addEntriesFromDictionary:input]; 

// Write Combined Dictionary to plist 
[dictionary writeToFile:path atomically:YES]; 

// Add Newly Created text to Table because reload table doesn't do it 
[myTableData addObject:newDevice.text]; 

// Reload Table Data even though it seems useless 
[myTable reloadData]; 


    } 

} 

답변

0

을 것입니다.

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Test.plist"]; 
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 

// Get exist input from plist 
NSMutableArray *inputFromPlist = dictionary[@"Room"]; 
// add new object to input 
[inputFromPlist addObject:newDevice.text]; 
// set Room with modified object 
[dictionary setObject:inputFromPlist forKey:@"Room"]; 
// Write Combined Dictionary to plist 
[dictionary writeToFile:path atomically:YES]; 
// do something after saving plist ... 
+0

아아아, 감사합니다. 의도 한대로 작동합니다. – Jesse

관련 문제