gpt4 book ai didi

iphone - 如何以附加模式写入文本文件

转载 作者:行者123 更新时间:2023-12-03 18:19:08 25 4
gpt4 key购买 nike

我的应用程序基于导航。 UItextView 用于注释 UIViewController。我正在将文本数据写入文件。现在我需要在附加模式下编写,我正在尝试以下代码,但每次都使用相同的文本数据写入两次,并且不附加下一个文本数据到文件。

- (void)saveText:(id)sender
{
[self.textview resignFirstResponder];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
NSString *savedString = textview.text;
[savedString writeToFile:documentTXTPath atomically:YES];


NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:documentTXTPath ];
[myHandle seekToEndOfFile];
[myHandle writeData: [savedString dataUsingEncoding:NSUTF8StringEncoding]];
[myHandle closeFile];

}

最佳答案

删除以下代码行

[savedString writeToFile:documentTXTPath atomically:YES];

剩下的代码都很好。无论如何也检查我的代码,

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"Notes.txt"];
NSString *savedString = textview.text;
NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:documentTXTPath];
[myHandle seekToEndOfFile];
[myHandle writeData:[savedString dataUsingEncoding:NSUTF8StringEncoding]];

关于iphone - 如何以附加模式写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4779877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com