3

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…".

jemmons
  • 18,605
  • 8
  • 55
  • 84

1 Answers1

5

Turns out -stringWithFormat: is the right way to go. I just need to use %C as the substitution instead of %d. NSLineSeparatorCharacter is an enumeration (and thus, integral), so the compiler thinks %d is what I should be using. But %C is Cocoa's way of inserting unichar types. With a little casting...

NSLog(@"This is%Con two lines…", (unichar)NSLineSeparatorCharacter);

Works like a charm!

Also note Ken's comment below: you can embed the character directly in a string literal using an escape sequence:

@"This is\u2028on two lines…"
jemmons
  • 18,605
  • 8
  • 55
  • 84