Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 1.1 KB

7.9 字符串的性能.md

File metadata and controls

36 lines (25 loc) · 1.1 KB

字符串

7.9 字符串的性能

Swift String中有很多字符类型的属性 如utf16(utf16View类型),utf8(utf8View类型),unicodeScalars(UnicodeScalarsView类型),characters(已废弃)

知识点:为什么是以View结尾的?

在api文档中对他们的介绍是:字符串所包含的编码单元的视图 (A view of a string's contents as a collection of code units. )

/// 你可以用不同的编码去遍历字符串中的每一个字符

  for codeUnit in "liaoWorking".utf16 {
      print("\(codeUnit) ")
  /**
      108
      105
      97
      111
      87
      111
      114
      107
      105
      110
      103
   */
  }

其中不同的字符类型属性效率不同 utf16效率最高

书中提到如果你能确保自己所做的操作可以正确处理 UTF-16 的数据,那选用 UTF-16 视图将会给你带来相当不错的性能提升

这里有一篇关于字符串不错的文章,推荐给大家。