写入文件不起作用
当我尝试写入文件与此代码:
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
那是因为你正在写主包,试试这个:
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"default.txt"];
在这里,我们试图写我们的应用程序文档目录。 因为你不能写在主包上