'쓰고'에 해당되는 글 1건

  1. 2010.06.24 NSDictionary 쓰고 읽는 방법
아이폰어플개발정보2010. 6. 24. 11:36
NSDictionary 쓰고 읽는 방법

by nik 13. 5월 2009 13:49

// you should add empty file "hiscores" in your xCode project

 - (void)save{

 NSLog(@"saving...");

  NSDictionary *hiscores;

 //adding object and keys;

 //...

 //...

 NSString *writableFilePath = [self createEditableCopyOfFileIfNeeded:[NSString stringWithString:@"hiscores"]]; 

 if (![hiscores writeToFile:writableFilePath atomically:YES]){

  NSLog(@"WRITE ERROR");

 }

 }

- (void)load{

 NSLog(@"loading...");

 NSString *writableFilePath = [self createEditableCopyOfFileIfNeeded:[NSString stringWithString:@"hiscores"]];

 hiscores = [NSDictionary arrayWithContentsOfFile:writableFilePath];

}

- (NSString *)createEditableCopyOfFileIfNeeded:(NSString *)_filename {

    // First, test for existence.

    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;

 
 // The writable file does not exist, so copy the default to the appropriate location.

 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;

}



출처 : http://blog.naver.com/PostView.nhn?blogId=amoros21&logNo=140107111174
Posted by 오늘마감

댓글을 달아 주세요