开发者

Writing to a new line of a file in Objective-C

开发者 https://www.devze.com 2022-12-27 05:10 出处:网络
For some strange reason, the \\n and \\r escape sequences do not seem to be working in my code.I want to write each NSString on a new line of the file, but it just appends it the last string on one li

For some strange reason, the \n and \r escape sequences do not seem to be working in my code. I want to write each NSString on a new line of the file, but it just appends it the last string on one line. Here's the code:

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];
}

Am I doing something wrong? Forgive me as I am new to Objective-C.


Your problem is this:

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

It should be:

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

精彩评论

暂无评论...
验证码 换一张
取 消