Obj-C:涉及 UTF-8 文字的 Unicode 字符组合问题
Obj-C: Issue with Unicode character composition involving UTF-8 literals
我遇到了在 Obj-C 中编写 Unicode 字符的问题,下一个示例代码对此进行了描述,它试图将 'e' 与尖音符结合起来:
NSLog(@"Composing with Unicode literal: '%@'\nComposing with UTF-8 literal: '%@'",
[[NSString stringWithUTF8String:"e\u0301"]
precomposedStringWithCanonicalMapping],
[[NSString stringWithUTF8String:"e\xc2\xb4"] // "\xc\xb4" is UTF-8 rep of "\u0301"
precomposedStringWithCanonicalMapping]);
输出为:
Composing with Unicode literal: 'é'
Composing with UTF-8 literal: 'e´'
所以只有当尖音符号被指定为 \u 文字时,代码才会产生正确的结果,而使用 UTF-8 表示似乎会产生错误的结果。我的问题:有没有办法使用 UTF-8?
您对组合重音的 UTF-8 编码有误。
将 \xc2\xb4
更改为 \xcc\x81
。此更改将为您带来预期的结果。
您在非组合口音中使用的口音。
您在组合时使用了错误的重音符:
NSString *utf = [[NSString stringWithUTF8String:"e\xcc\x81"] precomposedStringWithCanonicalMapping]; // "\xc\xb4" is UTF-8 rep of "\u0301"
NSLog(@"utf: %@",utf);
输出:
utf: é
我遇到了在 Obj-C 中编写 Unicode 字符的问题,下一个示例代码对此进行了描述,它试图将 'e' 与尖音符结合起来:
NSLog(@"Composing with Unicode literal: '%@'\nComposing with UTF-8 literal: '%@'",
[[NSString stringWithUTF8String:"e\u0301"]
precomposedStringWithCanonicalMapping],
[[NSString stringWithUTF8String:"e\xc2\xb4"] // "\xc\xb4" is UTF-8 rep of "\u0301"
precomposedStringWithCanonicalMapping]);
输出为:
Composing with Unicode literal: 'é'
Composing with UTF-8 literal: 'e´'
所以只有当尖音符号被指定为 \u 文字时,代码才会产生正确的结果,而使用 UTF-8 表示似乎会产生错误的结果。我的问题:有没有办法使用 UTF-8?
您对组合重音的 UTF-8 编码有误。
将 \xc2\xb4
更改为 \xcc\x81
。此更改将为您带来预期的结果。
您在非组合口音中使用的口音。
您在组合时使用了错误的重音符:
NSString *utf = [[NSString stringWithUTF8String:"e\xcc\x81"] precomposedStringWithCanonicalMapping]; // "\xc\xb4" is UTF-8 rep of "\u0301"
NSLog(@"utf: %@",utf);
输出:
utf: é