2010-07-01 2 views
2

내 writeToFile이 내 데이터를 내 .plist에 저장하지 않습니다. 여기iphone - writeToFile plist에 새 항목을 저장하지 않습니다.

- (IBAction)clickBtnDone:(id) sender { 
NSLog(@"Done"); 
if ([txtGroupName.text length] > 0) { 
    [self dismissModalViewControllerAnimated:YES]; 
    NSLog(@"Group Name: %@", txtGroupName.text); 
    NSMutableArray *newDict = [[NSMutableArray alloc] init]; 
    [self.groups setObject:newDict forKey:txtGroupName.text]; 
    NSLog(@"Count:%d", [self.groups count]); 

    BOOL success = [self.groups writeToFile:self.groupPath atomically:YES]; 
    if(success) { 
    NSLog(@"Success Saving New Group"); 
    } else { 
    NSLog(@"Failure Saving New Group"); 
    } 
    [newDict release]; 
} 
} 

디버그 보여줍니다 것입니다 그러나

2010-07-01 00:48:38.586 Contacts[7111:207] Done 
2010-07-01 00:48:38.589 Contacts[7111:207] Group Name: C 
2010-07-01 00:48:38.590 Contacts[7111:207] Count:3 
2010-07-01 00:48:38.592 Contacts[7111:207] Success Saving New Group 

, 나는 .plist 파일을 열 때, 그것은 여전히 ​​새 항목을 내가 직접 만들었다 단지 2 그룹이 아닌.

파일은 내 ~ 문서 폴더에 있습니다.

아이디어가 있으십니까?

+0

그룹 경로 란 무엇입니까? 인쇄 할 수 있습니까? – vodkhang

+0

NSString * 경로 = [[NSBundle mainBundle] pathForResource : @ "그룹"ofType : @ "plist"]; self.groupPath = path; – john

+0

글쓰기 :/Users/Me/Library/Applications 지원/iPhone Simulator/4.0/Applications /.../ Groups.plist .plist에 저장할 수있는 방법이 있나요? 내 작업 공간? – john

답변

3

groupPath를 어떻게 초기화 했습니까? 자원 디렉토리의 경로가 아닌 문서 디렉토리의 경로 여야합니다.

당신은 비슷한 수행해야합니다

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; 

당신은 작업 공간에 존재하는 파일을 편집 할 수 있습니다.

0
NSString* plistPath = nil; 
NSFileManager* manager = [NSFileManager defaultManager]; 
if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"PathTo.plist"])) 
    { 
    if ([manager isWritableFileAtPath:plistPath]) 
    { 
     NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; 
     [infoDict setObject:@"foo object" forKey:@"fookey"]; 
     [infoDict writeToFile:plistPath atomically:NO]; 
     [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil]; 
    } 
    } 
관련 문제