Swift笔记(一):Extended Grapheme Clusters
来源:互联网 发布:语c kg是什么意思网络 编辑:程序博客网 时间:2024/06/06 23:58
前言
简书地址:http://www.jianshu.com/p/ccde89ad9c1b
最近在看苹果官方swift文档《The Swift Programming Language》,记录一些笔记。
Extended Grapheme Clusters
swift使用了Extended Grapheme Clusters作为Character的显示。内容如下:
Every instance of Swift’s Character
type represents a single extended grapheme cluster. An extended grapheme cluster is a sequence of one or more Unicode scalars that (when combined) produce a single human-readable character.
具体的定义可以参考unicode的标准文档Grapheme Cluster Boundaries:
Unicode标准提供了算法去定义grapheme cluster boundaries,其中包括两种变种:legacy grapheme clusters and extended grapheme clusters。
A legacy grapheme cluster is defined as a base (such as A or カ) followed by zero or more continuing characters. One way to think of this is as a sequence of characters that form a “stack”.
An extended grapheme cluster is the same as a legacy grapheme cluster, with the addition of some other characters. The continuing characters are extended to include all spacing combining marks, such as the spacing (but dependent) vowel signs in Indic scripts.
具体的计算方式可以从文章中进行详细的了解。
举例
拿一个Apple文档里的例子:
let precomposed: Character = "\u{D55C}" // 한let decomposed: Character = "\u{1112}\u{1161}\u{11AB}" // ᄒ, ᅡ, ᆫ// precomposed is 한, decomposed is 한
韩文的音节可以拆分和组合,上面的两个String就是相同的String。
因为这种编码方式的问题,Swift想取一个String的字符个数,需要使用"".characters.count
的方式,获取character,再获取chara的个数。
Swift这边的String用的是21bit Unicode scalar字符编码方式(相当于UTF-32),而OC中的NSString用的是UTF-16字符编码方式。
所以对同一个String,转化为NSString,可能获得的长度方式也不同:
var str = "Hello ��" // the square is an emojistr.characters.count // returns 7(str as NSString).length // returns 8
就是现在所见非所得了,所以在处理swift string与nsstring转化时,要注意一下unicode的编码和长度问题。
(这边如果你是csdn看的话,你会发现这个emoji表情再csdn这会转化为两个?号。这也就是csdn的页面编码方式不支持emoji)
参考资料
1.The Swift Programming Language
2.Why is Swift counting this Grapheme Cluster as two characters instead of one?
3.Grapheme Cluster Boundaries
- Swift笔记(一):Extended Grapheme Clusters
- Swift笔记(一)
- Swift学习笔记(一)
- 学习Swift笔记 (一)
- swift学习笔记(一)
- Swift进阶笔记(一)
- Swift学习笔记(一)——Swift的数据类型
- swift 笔记(一)—— 基础
- Swift学习笔记一(基础部分)
- Swift学习笔记一(变量)
- 《Swift权威指南》学习笔记(一)
- Swift学习笔记(一) Optional
- swift学习笔记之函数(一)
- swift学习笔记_iOS开发(一)
- 大熊的swift学习笔记(一)
- Swift语言基础笔记(一)
- Swift语言学习笔记(一)
- Swift学习笔记一
- 14. Longest Common Prefix
- java8学习总结1
- Spring+SpringMVC+Freemarker(自定义标签)+Mybatis+Log4j快速整合
- Maven -- 常用插件介绍
- dhtmlx实现翻页的代码实现
- Swift笔记(一):Extended Grapheme Clusters
- STL—transform
- hadoop动态增加节点
- 计数排序
- 设备树(一)linux内核主线了解dts
- Unity根据名称递归查找一个物体的子物体
- 对linux根目录下各文件夹的初步认识
- PlaySound函数
- Google ProtoBuff 获取、编译、使用示例