I am trying to run some sample code and am having trouble creating and saving to a file.
Here is my code:
-(IBAction) example
{
NSFileManager *fm = [[NSFileManager alloc] init];
NSURL *docsurl = [fm URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
myfolder = [docsurl URLByAppendingPathComponent:@"MyFolder"];
NSError * err = nil;
BOOL ok = [fm createDirectoryAtURL:myfolder withIntermediateDirectories:YES attributes:nil error:&err];
ok = [label.text writeToURL:myfolder atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
-(IBAction) load
{
label2.text = [[NSString alloc] initWithContentsOfURL:myfolder
encoding:NSUTF8StringEncoding error:nil];
}
I want it to save the first label’s text to the file when I push the first button. When I push the second button, I want it to read what is in the file and make the second label text that. However when I push the second button, the second label just goes blank.
Any suggestions?
I suggest actually using those NSError objects. I also suggest using the default NSFileManager.
This code will not work as is, but it will print out whatever errors you are encountering which should help you solve the problem.