Ios 写入XCode项目目录中的文件

Ios 写入XCode项目目录中的文件,ios,file,directory,save,Ios,File,Directory,Save,我有一个包含在我的XCode项目中的文件,我想覆盖它。该文件在名为book的目录中称为Introduction.html 在屏幕截图中,您可以看到它在目录中的位置以及我对它的各种写入尝试 任何帮助都很好,谢谢 “编辑我的代码”在图片中不可读: //Atempt 1 NSString *myString = [mutableArray objectAtIndex:0]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumen

我有一个包含在我的XCode项目中的文件,我想覆盖它。该文件在名为book的目录中称为Introduction.html

在屏幕截图中,您可以看到它在目录中的位置以及我对它的各种写入尝试

任何帮助都很好,谢谢

“编辑我的代码”在图片中不可读:

 //Atempt 1
NSString *myString = [mutableArray objectAtIndex:0];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"/book/Introduction.html"];
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];

//Attempt 2
[myString writeToFile:@"/book/Introduction.html" atomically:NO encoding:NSUTF8StringEncoding error:&error];

//Attempt 3
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths2 objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: @"book/Introduction.html"];    
[myString writeToFile: docFile atomically:NO encoding:NSUTF8StringEncoding error:&error];


//Attempt 4
NSFileHandle *file;
NSMutableData *data;
const char *bytestring = "black dog";    
data = [NSMutableData dataWithBytes:bytestring 
                             length:strlen(bytestring)];
file = [NSFileHandle fileHandleForUpdatingAtPath: 
        @"book/Introduction.html"];
if (file == nil)
    NSLog(@"Failed to open file");
[file seekToFileOffset: 10];
[file writeData: data];
[file closeFile];
[file release];
itgiawa

就我所阅读的文档而言,“我的XCode项目”中的文件无法覆盖,因为iOS中的沙箱机制不允许你的应用程序访问此空间。我记得读过指令“如果需要覆盖应用程序附带的数据,请将其复制到Documents目录并覆盖副本。”

因此,我想说您的尝试1最接近,请先保存您需要将文件从资源包复制到Documents目录的事实

你好,nobi