質問

I finished writing a little program, which is able to read and write a/into a .txt file. When I execute the program, everything is running fine except that the content of the file doesn't change permanently. I got a writeToFile and "readFile" button and the content seems to change every time I press one of them, but when I open the file manually (while testing or after shutting down the program) theres still the origin content in it. Doesn't the "real" file content change while just using the simulator? Or is it just me making some bad mistakes?

-(IBAction)buttonPressed {  //The writeToFile Method
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSString *writeData = enterText.text;
NSError *error;
BOOL ok = [writeData writeToFile:filePath atomically:NO encoding:NSUnicodeStringEncoding error:&error]; 
if (!ok)
{
    NSLog(@"Error while writing file at %@/n%@",filePath,[error localizedFailureReason]);
}
testText.text =@"File saved!";
enterText.text = @"";
enterText.placeholder =@"Enter your text here";

}

testText = TextView for Output

enterText = TextField for Input

役に立ちましたか?

解決

Your filePath variable is pointing to a file within the resource bundle of your app (which is not writable). What you need to do is locate the user's Documents folder, and create your file there.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top