目标C:以编程方式将文件写入Main Bundle
可能重复:
写入文件目标c
我如何创build一个文件并写入这个包? 我有这个:
//make path to file NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"usnews.txt", [sharedManager.articleNumber intValue] + 1]]; //write file [textContent writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
但它将其写入文档文件夹,并在应用程序closures时被删除。
你不能写入iOS上的主包。 其内容作为App Store提交过程的一部分进行encryption签名; 修改其内容将阻止应用程序运行。
应用程序closures时不应删除文档。 尝试传递一个NSError
来接收你写的错误信息。 另外,您确定该文件正在写入您认为的位置吗? 你用这个文件名:
[NSString stringWithFormat:@"usnews.txt", [sharedManager.articleNumber intValue] + 1]
但是在其中没有%d
格式说明符,因此该数字从不插入到文件名中。
我很确定你可以。 写入资源文件夹是不被禁止的。
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"];
但就我所知,根据苹果文档,这不是一个好的行为