나름 프로그래밍?/ Objective-C

프로퍼트 리스트 기록, 읽기

알 수 없는 사용자 2010. 2. 11. 11:36
컬렉션 프로퍼티 리스트 클래스 (NSArray, NSDictionary)는 프로퍼티 리스트를 파일로 기록해주는

- writeToFile: atomically: 메소드를 가지고 있다.

NSArray *phrase;
    phrase = [NSArray arrayWithObjects: @"Hello", @"my", @"name", @"is", @"jesica", nil];
    [phrase writeToFile: @"/tmp/test.txt"  atomically: YES];

위 소스는 xml 파일 형식으로 Hello, my, name, is, jesica 를 순차적으로 저장한다.

저장된 test.txt 파일은 + arrayWithContentsOfFile: 메소드로 읽을 수 있다.

NSArray *phrase2 = [NSArray arrayWithContentsOfFile: @"/tmp/test.txt"];
    NSLog (@"%@", phrase2);

이들 함수의 단점은 에러 정보를 전혀 반환하지 않는다.

파일을 읽을 수 없으면 nil 포인터를 받게 될 것이다.