2012-05-23 2 views
1

.plist 파일에 아주 간단한 데이터를 쓰려고하는데 모든 종류의 코드 샘플을 검사했지만 고생하고 있습니다. 그것을 작동 시키려면..plist 파일에 쓰기를 시도했지만 작동하지 않습니다.

-(NSString *)getFilePath{ 
    NSArray *thePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    return [[thePath objectAtIndex:0] stringByAppendingPathComponent:@"events.plist"]; 
} 

다음이 내가 버튼에 매여 한 작업입니다 :

첫째, 경로를 파일 자체를 생성하고 반환하는 방법이있다. 현재 더미 데이터 만 삽입합니다. 적어도 그게 내가하려는 일이야.

- (IBAction)saveData:(id)sender { 
    NSArray *data = [[NSArray alloc] initWithObjects:@"E3 Expo", @"05-06-2012", nil]; 
    [data writeToFile:[self getFilePath] atomically:YES]; 
} 

하지만 작동하지 않습니다. Xcode에서 Organizer를 통해 사용자 문서의 디렉토리를 검사했는데 "Documents"폴더에 아무 것도 생성되지 않았습니다.

도움을 주시면 감사하겠습니다. :)

+0

속성 목록 ... 그것은 작동하지 않습니다 – CodaFi

답변

0

사전을 루트 plist 개체로 설정해야합니다.

[편집 : 당신이 NSFileManager 그것을 만들 파일을 볼 수없는 경우]

NSError *error; 
NSString *plistPath = [self getFilePath]; 

NSArray *data = [[NSArray alloc] initWithObjects:@"E3 Expo", @"05-06-2012", nil]; 
NSDictionary *plistDict = [NSDictionary dictionaryWithObject: data forKey: @"array"]; 
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict 
                   format:NSPropertyListXMLFormat_v1_0 
                errorDescription:&error]; 
if(plistData) { 
    //not necessary 
    if ([[NSFileManager defaultManager] fileExistsAtPath: plistPath]) { 
     [[NSFileManager defaultManager] removeItemAtPath: plistPath error: &error]; 
    } 
    [[NSFileManager defaultManager] createFileAtPath: plistPath contents:plistData attributes: nil]; 
    //necessary 
    [plistData writeToFile: plistPath atomically:YES]; 
} 
else { 
    NSLog(error); 
    [error release]; 
} 
//also release data, since you retained it (sorry, memory management OCD) 
[data release]; 

Based on Apple documentation - scroll to Write Out the Property List

+0

을 NSDictionarys 있습니다. 모든 코드가 실행 중이지만 문서 디렉토리는 완전히 비어 있습니다. – user1191304

+1

적절한 plist가 아니지만 작동해야합니다. [writeToFile : atomically :] (https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/writeToFile : atomicically :) : 배열의 내용이 모두 속성 목록 객체 (NSString, NSData, NSArray 또는 NSDictionary 객체) 인 경우이 메서드로 작성된 파일을 사용하여 클래스 메서드 인 arrayWithContentsOfFile : 또는 인스턴스 메서드를 사용하여 새 배열을 초기화 할 수 있습니다 initWithContentsOfFile :. –

+0

NSFileManager가 필요한 파일을 만드는 것이 필요하지 않지만 편집 된 답변을 참조하십시오. 나는 그것을 테스트하고 iExplorer로 파일을 가져 와서, 괜찮을 것 같다. –

관련 문제