我想在我的 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/1FFD4436-DCCA-4280-9E47-F6474BEE0183/MyFileApp.app/myFile.txt
为什么?
谢谢你的建议
最佳答案
你问:
Why?
这是那个路径,因为那是存储文件的地方。
在设备上会有所不同。
请注意,无论如何您都无法将文件写入该文件夹。您也许应该改为写入应用程序的文档文件夹:
//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];
关于objective-c - iOS : I can't write my file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11016371/