如何在 iOS sdk 的本地资源 txt 文件中附加字符串字符串、文件、如何在、资源

2023-09-07 09:56:18 作者:像中枪一样

我一直在尝试将字符串附加到本地资源文件,但找不到解决方案.我正在尝试为我的应用程序中的所有函数调用创建一个日志文件,因此如果它崩溃,我可以看到它停止在哪个函数上.

I have been trying to append strings to a local resource file but I am having trouble finding a solution. I am trying to create a log file for all the function call in my application so if it crashes I can see which function it stopped on.

我创建了一个 log.rtf 文件,但无法写入该文件.有人可以帮我在这个文件中附加一个字符串,而不必覆盖整个文件吗?

I have created a log.rtf file, but am not able to write in this file. Can someone please help me append a string to this file without having to overwrite the entire thing?

推荐答案

我使用以下代码来解决上述问题.

I have use following code for the above problem.

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *logPath = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingPathComponent:@"log.rtf"]];
NSFileHandle *fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:logPath];
[fileHandler seekToEndOfFile];
[fileHandler writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandler closeFile];