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

For some strange reason, the escape sequences \ n and \ r do not seem to work in my code. I want to write each NSString in a new line in the file, but it just adds the last line in one line. Here is 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.

+4
source share
1 answer

Your problem is this:

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

It should be:

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

Source: https://habr.com/ru/post/1307053/


All Articles