开发者

writeToFile doesn't change the content of the file xcode4 objective-c

开发者 https://www.devze.com 2023-04-01 09:56 出处:网络
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 perm

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开发者_StackOverflow中文版 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号