3

I was using these unicode definitions for sharp and flat symbols and they work fine in string concats:

#define kSharpSymbol [NSString stringWithFormat:@"\U0000266F"]
#define kFlatSymbol [NSString stringWithFormat:@"\U0000266D"]

[...]
// Set F#
[f setNoteLetterName:[NSString stringWithFormat:@"F%@",kSharpSymbol]];

Then, I just read on a SO question that relying on the unicode formatting is not recommended by Apple so I went to this, which also works but results in compiler warnings when I do the implicit string concat:

Format specifies type 'unsigned short' but the argument has type 'int'

#define kSharpSymbol [NSString stringWithFormat:@"%C", 0x266F]
#define kFlatSymbol [NSString stringWithFormat:@"%C", 0x266D]
[...]
// Set F#
[f setNoteLetterName:[NSString stringWithFormat:@"F%@",kSharpSymbol]];

I guess I need some clarity on this. What's best and how do I get the compiler to be happy?

Community
  • 1
  • 1
Slinky
  • 5,662
  • 14
  • 76
  • 130
  • 2
    Can you just cast `0x266F` to `(unsigned short)` in your latter example? – Tim Feb 15 '13 at 17:52
  • Did not work for me when I tried it – Slinky Feb 15 '13 at 18:07
  • Correction - This did work: #define kSharpSymbol2 [NSString stringWithFormat:@"%C", (unsigned short)0x266F] #define kFlatSymbol2 [NSString stringWithFormat:@"%C", (unsigned short)0x266D] – Slinky Feb 15 '13 at 18:09

1 Answers1

4

I would suggest another way to approach this problem: there is absolutely nothing wrong with using string constants that contain Unicode symbols directly, for example

#define kSharpSymbol @"♯"
#define kFlatSymbol  @"♭"

The advantage is that the human readers of your program are going to see the symbol without looking it up in a table. The disadvantage is that the program is not going to look correctly when viewed in some older text editors that do not support modern file encoding. Fortunately, Xcode's editor is not one of them, so it shouldn't be a concern.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523