Concatenating and saving musical symbols - Objective-C

I have used these unicode definitions for sharp and flat characters and they work great on concats strings:

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

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

      

Then I just read a StackOverflow question that Apple does not recommend relying on unicode formatting, so I went for this, which also works, but results in a compiler warning when I execute the implicit concat line:

The format is "unsigned short", but the argument is "int"

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

      

I think I need some clarity on this. Which is better and how can I make the compiler happy?

+3


source to share


1 answer


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

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

      



The advantage is that readers from your program are going to see the symbol without looking at the table. The disadvantage is that the program will not look correct when viewed in some older text editors that do not support modern file encoding. Luckily, Xcode is not one of them, so this shouldn't be a concern.

+4


source







All Articles