- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
// 또는
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
// 먼저 빈 "test" 파일을 생성해 놓아야 한다.
- (void)save {
NSLog(@"생성...");
NSDictionary *test;
// 객체와 키 추가.
// [test setObject:[self 프라퍼티] forKey:@"키"];
// ...
NSString *writableFilePath = [self createEditableCopyOfFileIfNeeded:[NSString stringWithString:@"test"]];
if (![test writeToFile:writableFilePath atomically:YES]){
NSLog(@"쓰기 에러);
}
}
- (void)load {
NSLog(@"로딩...");
NSString *writableFilePath = [self createEditableCopyOfFileIfNeeded:[NSString stringWithString:@"test"]];
test = [NSDictionary arrayWithContentsOfFile:writableFilePath];
}
- (NSString *)createEditableCopyOfFileIfNeeded:(NSString *)_filename {
// 파일 존재 여부 확인.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableFilePath = [documentsDirectory stringByAppendingPathComponent: _filename ];
success = [fileManager fileExistsAtPath:writableFilePath];
if (success) return writableFilePath;
// 사용자의 Documents 디렉토리에 파일이 없는 경우, 앱의 resources 디렉토리에서 기본 데이터 파일을 가져온다.
NSString *defaultFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: _filename ];
success = [fileManager copyItemAtPath:defaultFilePath toPath:writableFilePath error:&error];
if (!success) {
NSLog([error localizedDescription]);
NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}
return writableFilePath;
}
'개발 > App Developer' 카테고리의 다른 글
XCode info.plist 아이폰,아이팟터치 주요항목 정리 (0) | 2010.09.01 |
---|---|
SQLite (0) | 2010.09.01 |
gdb 디버거 (0) | 2010.09.01 |
Disclosure indicator , detail Disclosure Button (0) | 2010.09.01 |
뷰 (0) | 2010.09.01 |