Reputation: 69
I'm working on an app that's a bit like a notebook of typed pages.
I have a UITextView *documentText
and an int currentPage
to keep track of multiple pages. Back and forward buttons add 1 or subtract 1 from currentPage
and then they set the text in documentText
to match the new value of currentPage
.
However, when I run it, nothing saves, there's just a blank text field. I think it's a problem with [saveDocs writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:NULL];
, but I'm not sure what to change, or if that's even the problem.
Anyways, here's the code I'm using to save the text:
- (IBAction)saveDocs:(id)sender {
NSString *saveDocs = documentText.text;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"%@-%d.txt", @"document", currentPage]];
[saveDocs writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:NULL];
[[NSUserDefaults standardUserDefaults] setInteger:currentPage forKey:@"CurrentDocument"];
}
Also, in ViewDidLoad, I'm using this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
currentPage = [[NSUserDefaults standardUserDefaults] integerForKey:@"CurrentDocument"];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"%@-%d.txt", @"document", currentPage]];
NSString* doc = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[documentText setText:doc];
Thanks for your help, -Karl
Upvotes: 0
Views: 390
Reputation: 14834
You write with NSUnicodeStringEncoding. But you read with NSUTF8StringEncoding. Perhaps you should stick with NSUTF8StringEncoding for both writing and reading.
Upvotes: 1