1

found a few things on this issue but so far nothing has worked.

I'm trying to write to the end of a file using the following code:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"txt"];
NSString *textToWrite = @"Test";
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[textToWrite dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];

When I step through the code filePath gets set to "/var/mobile/Applications/1669D96A-1ABA-4A4F-D083-A90EB728691B/MyApp.app/Filename.txt".

However the NSFileHandle fileHandleForWritingAtPath returns nil. The code doesn't raise an exception/warning and continues as though fine. When I read the file into a string the new text is not there, only the stuff that is initially in the file (typed in Xcode). I use the exact same code to get the path for reading the file, and that works!

4

1 回答 1

1

应用程序包是只读的。您不能写入包中的任何路径。这就是为什么你得到一个nil文件句柄。

于 2014-02-26T22:57:48.027 回答