objective-c - iOS : I can't write my 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/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/

相关文章:

objective-c - Objective C 中的多态方法

c - semop : Bad file descriptor

ios - 按下按钮时 UIViewcontroller 不打开

objective-c - valueForKeyPath 意外返回 nil

ios - 尝试从方法返回协议(protocol)关联类型时出错

ios - AdBanner 未在模拟器中显示

c# - 如何在使用 BinaryWriter & Reader 时有效地实现文件的哈希验证?

php - 使用 PHP 代码写入 PHP 文件?

ios - 实体描述值 ForUndefinedKey 此类与键 "KEY"的键值编码不兼容

c++ - 重复符号仅适用于 iOS 模拟器构建