Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating & storing music symbols - Objective-C

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?

like image 247
Slinky Avatar asked Nov 26 '25 19:11

Slinky


1 Answers

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.

like image 108
Sergey Kalinichenko Avatar answered Nov 28 '25 07:11

Sergey Kalinichenko