Reputation: 404
I want to create a document based application in Xcode. It's a note app. People can create their notes file. It's a basic app, because I'm trying to learn how you can save and load data in a document based application. I'm not trying to save a tableview, I'm trying to save and load the stringValue of a TextField.
So, how do you save and load a NSString in a document based app in Objective-C (Xcode)?
Upvotes: 2
Views: 184
Reputation: 759
You can use NSString's writeToFile:atomically:encoding:error:
and stringWithContentsOfFile:encoding:error:
methods.
Here's an example of how to write to a file:
NSString *fileName = "myNote.txt";
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = dirPaths[0];
NSString *filePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:fileName]];
[myString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&myError];
Or if you want something advanced, you can use Core Data.
Upvotes: 1