Created
January 5, 2018 11:55
-
-
Save prat14k/283bb06e81ac0dcf3419f002ba1ccc09 to your computer and use it in GitHub Desktop.
objc File Manupulation methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)writeStringToFile:(NSString*)aString { | |
// Build the path, and create if needed. | |
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
NSString* fileName = @"bookmark.json"; | |
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName]; | |
if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) { | |
[[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil]; | |
} | |
// The main act... | |
[[aString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO]; | |
} | |
- (NSString*)readStringFromFile { | |
// Build the path... | |
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
NSString* fileName = @"bookmark.json"; | |
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName]; | |
// The main act... | |
return [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment