我有一个包含在我的 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];