不应覆盖文件中的文本

时间:2010-08-25 08:43:22

标签: objective-c cocoa-touch file-io

我使用以下代码写入文件。

fileName = [[NSBundle mainBundle] pathForResource: @"user" ofType: @"txt"];
NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[myHandle seekToEndOfFile];

NSData *dataName = [uname dataUsingEncoding: NSUnicodeStringEncoding];

if ([dataName writeToFile:fileName atomically:YES])
    NSLog(@"writeok");

[myHandle seekToEndOfFile];

它会覆盖该文件。假设文件已包含字符串“box”。现在,当我写一个新单词“Hello”时,该文件只包含“Hello”,而不是“box”。

代码有什么问题?

1 个答案:

答案 0 :(得分:2)

您无法写入Bundle中的文件。由于Bundle是代码签名,任何尝试 更改捆绑包中的文件会破坏签名。您需要将文件移动到 Documents目录并在那里进行处理。

相关问题