当我尝试使用此代码写入文件时:
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);
什么都没发生。 NSLog说:
(null)
1
答案 0 :(得分:2)
那是因为你正在写主要包试试这个:
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"default.txt"];
这里我们试着写一下我们的app Documents目录。因为你不能写在主包上