问题
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