ios聲稱文件已被寫入,但更改從未實際保存,就好像它們保留在緩衝區中一樣。我必須沖洗什麼?ios writeToFile更改不保存
- (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
這是輸出每次都遇到我運行時間,所以變化不會真正得到寫入文件。
感謝您的幫助。
資源包是隻讀的。 –