Objective c iOS:我可以';我不能写我的文件

Objective c iOS:我可以';我不能写我的文件,objective-c,ios,file,Objective C,Ios,File,我想在我的iPhone应用程序上写一个文件。我的文件位于项目文件夹中,但当我尝试访问它时: NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"]; NSLog(@"%@",myFile); 我得到以下路径: 2012-06-13 17:36:56.398 MyFileApp[610:15203]/Users/Damian/Library/Application Support/iPh

我想在我的iPhone应用程序上写一个文件。我的文件位于项目文件夹中,但当我尝试访问它时:

NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"];
NSLog(@"%@",myFile);
我得到以下路径:

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

为什么?

谢谢你的建议

你问:

为什么?

它是该路径,因为文件存储在该路径中

在设备上,它将是不同的。

请注意,您无论如何都不能将文件写入该文件夹。您可能应该改为写入应用程序的“文档”文件夹:

//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会将项目复制到库目录中的文件夹中。。在您的情况下:

/用户/Damian/Library/Application Support/iPhone Simulator/5.1/Applications/

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

请注意,您永远不应该——在iOS和Mac OS X上都不应该——写入应用程序包。