// 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;
}