3

我将这些 un​​icode 定义用于尖锐和扁平的符号,它们在字符串连接中工作正常:

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

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

然后,我刚刚阅读了一个SO question,Apple 不推荐依赖 unicode 格式,所以我去了这个,它也可以工作,但是当我执行隐式字符串 concat 时会导致编译器警告:

格式指定类型“无符号短”,但参数的类型为“int”

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

我想我需要澄清一下。什么是最好的,如何让编译器满意?

4

1 回答 1

4

我建议用另一种方法来解决这个问题:例如,直接使用包含 Unicode 符号的字符串常量绝对没有错

#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.

于 2013-02-15T17:53:08.633 回答