【问题标题】:iOS : I can't write my fileiOS:我无法写入我的文件
【发布时间】:2012-06-13 13:53:13
【问题描述】:

我想在我的 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/我的文件.txt

为什么?

感谢您的建议

【问题讨论】:

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

标签: objective-c ios file


【解决方案1】:

你问:

为什么?

就是那个路径,因为那是文件的存储位置。

在设备上会有所不同。

请注意,无论如何您都不能将文件写入该文件夹。您或许应该改为写入应用的文档文件夹:

//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];

【讨论】:

    【解决方案2】:

    如前所述,您无法在实际设备上写入主包。

    关于你的另一个问题:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多