NSString is pretty unicode friendly. So normally when I want to create a string containing unicode, I can pop it directly into a string literal like so:
NSString *myString = @"Press ⌘Q to quit…";
But that doesn’t work with using a line separator (AKA:
NSLineSeparatorCharacter, Unicode U+2028, UTF-8 E2 80 A8). The compiler (correctly) interprets this as a line break, which is a no-no in C syntax.
-stringWithFormat: doesn’t help either. Trying
NSString *myString = [NSString stringWithFormat@"This is%don two lines…", NSLineSeparatorCharacter];
gives me the string “This is8232on two lines…”.
Turns out
-stringWithFormat:is the right way to go. I just need to use%Cas the substitution instead of%d.NSLineSeparatorCharacteris an enumeration (and thus, integral), so the compiler thinks%dis what I should be using. But%Cis Cocoa’s way of insertingunichartypes. With a little casting…Works like a charm!
Also note Ken’s comment below: you can embed the character directly in a string literal using an escape sequence: