I need to save NSStrings from an array into a .txt file in my documents directory, which is the easy part. but how do I put a new linebreak between each string?
i have already tried \n as you can see in the code below, which works fine when showing the new string in NSLog. But when you open the .txt on my PC it is one continuous line. My head is now spinning in circles, someone please save me lol
NSString *string = [stringArray componentsJoinedByString:@"\n"];
NSLog(@"string: %@",string);
NSData *dataToWrite = [[NSString stringWithFormat:@"%@",string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"myFile.txt"];
[dataToWrite writeToFile:path atomically:YES];
The “\n” character is a newline, but does not create a carriage return as well. On some systems (Unix systems in particular), a newline is all you need. On Windows machines, however, it frequently expects a carriage return (\r) followed by a newline (\n)
Note that some applications on Windows are smart enough to handle just a newline (\n) character without a carriage return (\r) and still display it correctly. For example, I think that while notepad does NOT render newline-only documents with line breaks, wordpad does.