Objective-C如何获取unicode字符

标签 objective-c unicode

我想获取 Objective-C 中给定 unicode 字符的 unicode 代码点。 NSString 表示它内部使用 UTF-16 编码并表示,

The NSString class has two primitive methods—length and characterAtIndex:—that provide the basis for all other methods in its interface. The length method returns the total number of Unicode characters in the string. characterAtIndex: gives access to each character in the string by index, with index values starting at 0.

这似乎假设 characterAtIndex 方法是 unicode 感知的。然而它返回的 unichar 是一个 16 位无符号整型。

- (unichar)characterAtIndex:(NSUInteger)index

问题是:

  • Q1:UFFFF以上的unicode码位是如何呈现的?

  • 问题 2:如果问题 1 有意义,是否有方法可以在 Objective-C 中获取给定 unicode 字符的 unicode 代码点。

谢谢。

最佳答案

“问题 1:它如何在 UFFFF 之上呈现 unicode 代码点?”的简短回答是:你需要是UTF16了解并正确处理Surrogate Code Points .下面的信息和链接应该为您提供指导和示例代码,以帮助您执行此操作。

NSString文档是正确的。然而,当你说“NSString 说它内部使用 UTF-16 编码”时,更准确地说是 NSString 的公共(public)/抽象接口(interface)。是UTF16基于。不同之处在于,这将字符串的内部表示保留为私有(private)实现细节,但公共(public)方法如 characterAtIndex:length总是在 UTF16 .

这样做的原因是它倾向于在较旧的 ASCII 之间取得最佳平衡。 -centric 和 Unicode 感知字符串,主要是因为 Unicode 是 ASCII 的严格超集。 (ASCII 使用 7 位,用于 128 个字符,映射到前 128 个 Unicode 代码点)。

表示 Unicode Code Points那是 > U+FFFF ,这显然超出了单个 UTF16 中可以表示的范围Code Unit , UTF16 使用特殊 Surrogate Code Points形成一个Surrogate Pair , 组合在一起形成一个 Unicode 代码点 > U+FFFF .您可以在以下位置找到有关此内容的详细信息:

关于Objective-C如何获取unicode字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4726593/

相关文章:

ios - 从 webservice 获取的图像未显示在 viewcontroller 中

iphone - CGPoint 等价于整数?

ios - 需要将方法从 Objective-C 转换为 iOS 中的 C

iphone - 如何同步不同调度队列中的任务?

python - 如何在 Django 中用阿拉伯字符创建 slug?

R 字母支持 åäöü

python - API 调用 (json) 上的 UnicodeEncodeError

iphone 开发 : having darker background

javascript - XRegExp 通过工作 unicode 检查返回 false\p?

python - 获取字符串的unicode字符