Skip to content

Instantly share code, notes, and snippets.

@alekseypotapov-dev
Last active August 29, 2015 13:56
Show Gist options
  • Save alekseypotapov-dev/9246581 to your computer and use it in GitHub Desktop.
Save alekseypotapov-dev/9246581 to your computer and use it in GitHub Desktop.
Use this if you wonna to save .plist to Document directory and then wonna to edit this file
/*
Then just to use somehow like this (if array)
NSMutableArray *parsedPlist = [[NSMutableArray alloc] initWithContentsOfFile:[PlistReader copyFileToDocumentDirectory:%"filename.plist"]];
*/
+ (NSString *)copyFileToDocumentDirectory:(NSString *)fileName
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *documentDirPath = [documentsDir
stringByAppendingPathComponent:fileName];
NSArray *file = [fileName componentsSeparatedByString:@"."];
NSString *filePath = [[NSBundle mainBundle]
pathForResource:[file objectAtIndex:0]
ofType:[file lastObject]];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:documentDirPath];
if (!success) {
success = [fileManager copyItemAtPath:filePath
toPath:documentDirPath
error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable txt file file with message \
'%@'.", [error localizedDescription]);
}
}
return documentDirPath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment