ios 声称该文件已被写入,但更改从未真正保存,就好像它们保留在缓冲区中一样。我必须冲洗还是什么?
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:@"MyFile"
ofType:@"txt"];
NSLog(@"\n\nPath = \n%@", myFilePath);
NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"\n\nContents = \n%@", myFileContents);
[@"This line will be written to file" writeToFile:myFilePath
atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *changedContents = [NSString stringWithContentsOfFile:myFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"\n\nChanged Contents = \n%@", changedContents);
}
输出:
Path =
/Users/hamzeh/Library/Application Support/iPhone Simulator/5.1/Applications/2B318687-A745-4CD9-85BA-52A01AB76F6E/savepersistentdata_tutorial.app/MyFile.txt
Contents =
This is a text file.
The file will be displayed on the iPhone.
That is all for now.
Changed Contents =
This line will be written to file
这是我每次运行时得到的输出,因此更改实际上并没有写入文件。
谢谢您的帮助。