gpt4 book ai didi

objective-c - 在 Objective-C 中写入文件的新行

转载 作者:太空狗 更新时间:2023-10-30 03:57:42 24 4
gpt4 key购买 nike

由于某些奇怪的原因,\n 和\r 转义序列在我的代码中似乎不起作用。我想将每个 NSString 写在文件的新行上,但它只是将它附加到一行的最后一个字符串。这是代码:

for (Entry *entry in self.entries) {
NSString *path = @"/Users/me/File.txt";
NSString *string = (@"%@\r\n", [entry thisEntry]);
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:path];
[fh seekToEndOfFile];
[fh writeData:[string dataUsingEncoding:NSUnicodeStringEncoding]];
[fh closeFile];
}

我做错了什么吗?请原谅我,因为我是 Objective-C 的新手。

最佳答案

你的问题是这样的:

NSString *string = (@"%@\r\n", [entry thisEntry]);

应该是:

NSString *string = [NSString stringWithFormat:@"%@\r\n", [entry thisEntry]];

关于objective-c - 在 Objective-C 中写入文件的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2646935/

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