iOS:我不能写我的文件

我想在我的iPhone应用程序上写一个文件。 我的文件在我的项目文件夹中,但是当我试图达到它时:

NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"]; NSLog(@"%@",myFile); 

我得到以下path:

2012-06-13 17:36:56.398 MyFileApp [610:15203] / Users / Damian / Library / Application Support / iPhone Simulator / 5.1 / Applications / 1FFD4436-DCCA-4280-9E47-F6474BEE0183 / MyFileApp.app / myFile.txt

为什么?

感谢您的build议

你问:

为什么?

这就是那个path,因为那是存储文件的地方。

在设备上会有所不同。

请注意,您无法将文件写入该文件夹。 您也许应该写入您的应用程序的文档文件夹:

 //Get the documents folder NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; //Get the final path of your file. NSString *finalPath = @"myfile.txt"]; //And actually write that file. [[NSFileManager defaultManager] createFileAtPath:finalPath contents:/*fileData as NSData*/ attributes:nil]; 

如前所述,您不能写入实际设备上的主包。

对于你的另一个问题:

当您在模拟器中运行您的应用程序时,xcode会将您的项目复制到您的库目录中的一个文件夹中。在您的情况下:

/ Users / Damian / Library / Application Support / iPhone Simulator / 5.1 / Applications /

您在模拟器中运行的每个应用程序都有一个文件夹。 应用程序不会在您实际编辑您的代码的文件夹中运行。