Write to file does not work [duplicate]

余生颓废 提交于 2020-01-16 01:02:24

问题


When i try to write to file with this code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"default" ofType:@"txt"];
NSError *error = nil;
NSString*s = @"some text";
BOOL successful = [s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",error);
NSLog(@"%d",successful);

Nothing happens. NSLog says:

(null)
1

回答1:


That's because you're writing over the main bundle try this:

    NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"default.txt"];

Here we're trying to write over our app Documents directory. Because you can't write on the main bundle



来源:https://stackoverflow.com/questions/16200979/write-to-file-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!