SwiftのStringの文字の数え方を完全理解する

 SwiftのStringの文字の数え方を完全理解する

SwiftはUnicodeの扱いに非常に長けた言語であり、絵文字を含む文字列でも正しい文字数を計算してくれます。
その反面、Unicodeの複雑さに引きずられてしまい、直感的な操作ができない時もあります。たとえば、 string[2] と書いても3番目の文字を取得することはできません。

そんな複雑なところのあるSwiftの文字列処理ですが、複雑なものを受け入れてきちんと理解するのはそこまで難しいものではありません。

このトークでは、Unicodeとの関係を意識しながら、Swiftの文字数の扱い方とその裏にある考え方を解説します。

Da1305e5281b5208da85d14a356d01f3?s=128

Takanori Hirobe

September 05, 2019
Tweet

Transcript

  1. SwiftͷStringͷจࣈͷ ਺͑ํΛ׬શཧղ͢Δ Takanori Hirobe @taka1068

  2. • ኍ෦ وಙ • ϑΣϯϦϧגࣜձࣾॴଐ ࣗݾ঺հ

  3. ࣗݾ঺հ • ኍ෦ وಙ • ϑΣϯϦϧגࣜձࣾॴଐ ΰʔϧυεϙϯαʔ

  4. ࣗݾ঺հ • ኍ෦ وಙ • ϑΣϯϦϧגࣜձࣾॴଐ ΰʔϧυεϙϯαʔ Ϙτϧ΢Υʔλʔεϙϯαʔ

  5. ࣗݾ঺հ • ኍ෦ وಙ • ϑΣϯϦϧגࣜձࣾॴଐ ΰʔϧυεϙϯαʔ Ϙτϧ΢Υʔλʔεϙϯαʔ

  6. ΞδΣϯμ - SwiftͷStringʹ͍ͭͯ - Unicodeʹ͍ͭͯ - Stringͱཁૉͷஔ͖׵͑ʹ͍ͭͯ

  7. ΞδΣϯμ -SwiftͷStringʹ͍ͭͯ - Unicodeʹ͍ͭͯ - Stringͱཁૉͷஔ͖׵͑ʹ͍ͭͯ

  8. print("Hello, iOSDC!")

  9. String

  10. String Value Type Bridges to NSString

  11. String Value Type UTF-8 Encoded Hashable Bridges to NSString

  12. String Value Type UTF-8 Encoded Equatable Hashable Bridges to NSString

    Collection
  13. String Value Type UTF-8 Encoded Equatable Hashable Bridges to NSString

    Collection
  14. Collection

  15. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑"
  16. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" let first = array[0]
  17. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" let first = array[0] let first = array[array.startIndex] or better
  18. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" let first = array[0]
  19. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" let first = array[0] Int ഑ྻͷཁૉͷܕ
  20. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int
  21. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int let first = string[0]
  22. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int let first = string[0]
  23. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int let first = string[string.startIndex]
  24. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int let first = string[string.startIndex] String.Index Character
  25. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int let startIdx = str.startIndex let index = str.index(startIdx, offsetBy: 5) let fifthChar = str[index]
  26. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int let startIdx = str.startIndex let index = str.index(startIdx, offsetBy: 5) let fifthChar = str[index]
  27. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character
  28. Collection var array = [ , 20, 30, 40, 50]

    var string = “ bc͍͋͏͓͑” ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character 10 Λ 100 ʹม͑Δ a Λ X ʹม͑Δ a 10
  29. Collection X 100 var array = [ , 20, 30,

    40, 50] var string = “ bc͍͋͏͓͑” ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character
  30. Collection var array = [100, 20, 30, 40, 50] var

    string = "Xbc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character array[0] = 100
  31. Collection var array = [100, 20, 30, 40, 50] var

    string = "Xbc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character array[0] = 100 "
  32. Collection var array = [100, 20, 30, 40, 50] var

    string = "Xbc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character
  33. Collection var array = [100, 20, 30, 40, 50] var

    string = "Xbc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character string[string.startIndex] = "X"
  34. Collection var array = [100, 20, 30, 40, 50] var

    string = "Xbc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character string[string.startIndex] = "X" #
  35. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈʹΑΔมߋ: OK ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character ఴࣈʹΑΔมߋ: NG
  36. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈʹΑΔมߋ: OK ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character ఴࣈʹΑΔมߋ: NG
  37. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈʹΑΔมߋ: OK ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character ఴࣈʹΑΔมߋ: NG
  38. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈʹΑΔมߋ: OK ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character ఴࣈʹΑΔมߋ: NG count͸5 count͸8
  39. Collection let array = [10, 20, 30, 40, 50] let

    string = "abc͍͋͏͓͑" ఴࣈΞΫηε: Int ཁૉͷܕ: Int ఴࣈʹΑΔมߋ: OK ఴࣈΞΫηε: String.Index ཁૉͷܕ: Character ఴࣈʹΑΔมߋ: NG
  40. ಉ͡Collectionͱ͍ͬͯ΋ѻ͍ํ ͕গ͠ҧ͏ - Stringͷํ͕ѻ͍ʹ͍͘

  41. ΞδΣϯμ - SwiftͷStringʹ͍ͭͯ -Unicodeʹ͍ͭͯ - Stringͱཁૉͷஔ͖׵͑ʹ͍ͭͯ

  42. ʮͺʯͱ͍͏จࣈΛ
 UnicodeͰද͢ ྫ

  43. ͺ ͺ let pa1 = “…” let pa2 = “…”

    print(pa1, pa2) // ͺ ͺ
  44. ͺ ͺ let pa1 = “…” let pa2 = “…”

    print(pa1, pa2) // ͺ ͺ
  45. ͺ ͺ จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸

    U+306F 㿇 U+309A
  46. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  47. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A ʮͺʯΛද͢εΧϥ஋(͋Δछͷූ߸)୯ମͰʮͺʯΛද͢
  48. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  49. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A ʮ͸ʯͱʮ㿇 ʯΛ૊Έ߹ΘͤͯʮͺʯΛද͢ɻ
  50. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  51. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  52. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  53. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  54. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  55. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  56. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  57. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  58. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  59. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  60. จࣈ ͺ ͺ Unicode
 Scalar ͺ U+3071 ͸ U+306F 㿇

    U+309A
  61. None
  62. None
  63. None
  64. None
  65. จࣈ $ & Unicode
 Scalar U+1F469 U+200D U+1F692 U+1F3E3 U+1F1EF

    U+1F1F5
  66. จࣈ $ & Unicode
 Scalar U+1F469 U+200D U+1F692 U+1F3E3 U+1F1EF

    U+1F1F5
  67. ֦ுॻهૉΫϥελ

  68. ֦ுॻهૉΫϥελ • ӳޠͰ͸ Extended Grapheme Cluster

  69. https://developer.apple.com/documentation/swift/character

  70. https://developer.apple.com/documentation/swift/character

  71. https://developer.apple.com/documentation/swift/character

  72. https://developer.apple.com/documentation/swift/character

  73. ֦ுॻهૉΫϥελ • ӳޠͰ͸ Extended Grapheme Cluster • SwiftͷCharacterܕʹ֘౰

  74. ֦ுॻهૉΫϥελ • ӳޠͰ͸ Extended Grapheme Cluster • SwiftͷCharacterܕʹ֘౰ • Ϣʔβʔʹͱͬͯͷʮ1จࣈʯ

  75. "\r\n"͸1จࣈ

  76. https://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries

  77. งғؾΛײͯ͡ΈΔ

  78. งғؾΛײͯ͡ΈΔ "ͺa\r\n&"

  79. U+306F U+309A U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ ͺ

    a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  80. U+306F U+309A U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ ͺ

    a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 1 2 3 4
  81. U+306F U+309A U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ ͺ

    a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  82. U+309A U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F
  83. U+309A U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ
  84. U+309A U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ
  85. U+309A U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ
  86. U+309A: ʮ ʄʯ U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ

    ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ
  87. U+309A: ʮ ʄʯ U+0A U+1F146 U+200D U+1F692 U+61 U+0D จࣈ

    ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ Nonspacing Mark
  88. U+61 U+309A: ʮ ʄʯ U+0A U+1F146 U+200D U+1F692 U+0D จࣈ

    ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ
  89. U+61 U+309A: ʮ ʄʯ U+0A U+1F146 U+200D U+1F692 U+0D จࣈ

    ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ
  90. U+61: ʮaʯ U+309A: ʮ ʄʯ U+0A U+1F146 U+200D U+1F692 U+0D

    จࣈ ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ
  91. U+61: ʮaʯ U+309A: ʮ ʄʯ U+0A U+1F146 U+200D U+1F692 U+0D

    จࣈ ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 U+306F: ͻΒ͕ͳͷʮ͸ʯ ͺ
  92. U+61: ʮaʯ U+0A U+1F146 U+200D U+1F692 U+0D จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  93. U+61: ʮaʯ U+0A U+1F146 U+200D U+1F692 U+0D จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  94. U+61: ʮaʯ U+0A U+1F146 U+200D U+1F692 U+0D จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  95. U+61: ʮaʯ U+0A U+1F146 U+200D U+1F692 U+0D: Carriage Return จࣈ

    ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  96. U+61: ʮaʯ U+0A U+1F146 U+200D U+1F692 U+0D: Carriage Return จࣈ

    ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 a
  97. U+0A U+1F146 U+200D U+1F692 U+0D: Carriage Return จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  98. U+1F146 U+200D U+1F692 U+0D: Carriage Return U+0A จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  99. U+1F146 U+200D U+1F692 U+0D: Carriage Return U+0A จࣈ ͺ a

    \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  100. U+1F146 U+200D U+1F692 U+0D: Carriage Return U+0A: Line Feed จࣈ

    ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  101. U+1F146 U+200D U+1F692 U+0D: Carriage Return U+0A: Line Feed จࣈ

    ͺ a \r \n & Unicode
 Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 \r\n ͷ૊Έ߹Θͤ͸ ಛผѻ͍
  102. U+1F146 U+200D U+1F692 จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  103. U+200D U+1F692 U+1F1EF จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  104. U+200D U+1F692 U+1F1EF จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  105. U+200D U+1F692 U+1F1EF: จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 J
  106. U+200D U+1F692 U+1F1EF: จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 ࠃίʔυͷʮJʯ J
  107. U+200D U+1F1EF: U+1F1F5 จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 J
  108. U+200D U+1F1EF: U+1F1F5 จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 J
  109. U+200D U+1F1EF: U+1F1F5: จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 J P
  110. U+200D U+1F1EF: U+1F1F5: จࣈ ͺ a \r \n & Unicode


    Scalar U+306F U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 J P ࠃίʔυʮJʯ+ʮPʯ Ͱ೔ຊ
  111. U+200D จࣈ ͺ a \r \n & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5
  112. U+200D จࣈ ͺ a \r \n & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 1 2 3 4
  113. U+200D จࣈ ͺ a \r \n & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+0A U+1F1EF U+1F1F5 1 2 3 4 ΋͠\nͰ͸ͳͯ͘ʮbʯ ͩͬͨΒ
  114. U+200D จࣈ ͺ a \r b & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+62 U+1F1EF U+1F1F5 1 2 3 4
  115. U+200D จࣈ ͺ a \r b & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+62 U+1F1EF U+1F1F5 1 2 3 4 \r ͱ b ͸݁߹͠ͳ͍
  116. U+200D จࣈ ͺ a \r b & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+62 U+1F1EF U+1F1F5 1 2 3 4 5
  117. U+200D จࣈ ͺ a \r b & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+62 U+1F1EF U+1F1F5 1 2 3 4 5 4จࣈ໨ͩͬͨ΋ͷ͕ 5จࣈ໨ʹ
  118. U+200D จࣈ ͺ a \r b & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+62 U+1F1EF U+1F1F5 1 2 3 4 5 N൪໨ͷจࣈΛ஌ΔͨΊʹ͸ɺ ͦͷલʹ͋ΔจࣈΛ஌Δඞཁ͕͋Δɻ
  119. U+200D จࣈ ͺ a \r b & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+62 U+1F1EF U+1F1F5 1 2 3 4 5 N൪໨ͷจࣈΛ஌ΔͨΊʹ͸ɺ ͦͷલʹ͋ΔจࣈΛ஌Δඞཁ͕͋Δɻ ‑ N൪໨ͷจࣈΛऔಘ͢ΔͨΊͷܭࢉྔ͸O(N)
  120. U+200D จࣈ ͺ a \r b & Unicode
 Scalar U+306F

    U+309A U+61 U+0D U+62 U+1F1EF U+1F1F5 1 2 3 4 5 N൪໨ͷจࣈΛ஌ΔͨΊʹ͸ɺ ͦͷલʹ͋ΔจࣈΛ஌Δඞཁ͕͋Δɻ ‑ N൪໨ͷจࣈΛऔಘ͢ΔͨΊͷܭࢉྔ͸ O(N)
  121. ܭࢉྔ ܭࢉʹ͔͔Δ࣌ؒ จࣈͷҐஔ

  122. ܭࢉྔ ܭࢉʹ͔͔Δ࣌ؒ จࣈͷҐஔ O(N)

  123. String͸Collectionʹ४ڌ • ఴࣈΞΫηεͷܭࢉྔ͸O(1)Ͱͳ͍ͱ͍͚ͳ͍

  124. ܭࢉྔ ܭࢉʹ͔͔Δ࣌ؒ จࣈͷҐஔ O(N)

  125. ܭࢉྔ ܭࢉʹ͔͔Δ࣌ؒ จࣈͷҐஔ O(N) Collectionϓϩτίϧͷ ཁ੥

  126. ܭࢉྔ ܭࢉʹ͔͔Δ࣌ؒ จࣈͷҐஔ IntͰఴࣈΞΫηεͨ͠ͱ͖ʹ͔ ͔Δ࣌ؒ O(N) Collectionϓϩτίϧͷ ཁ੥ O(1)

  127. ܭࢉྔ ܭࢉʹ͔͔Δ࣌ؒ จࣈͷҐஔ IntͰఴࣈΞΫηεͨ͠ͱ͖ʹ͔ ͔Δ࣌ؒ O(N) Collectionϓϩτίϧͷ ཁ੥ O(1) String͸IntͰ͸ఴࣈΞΫηε

    Ͱ͖ͳ͍
  128. StringͷఴࣈΞΫηεʹ͍ͭͯ

  129. String.Index StringͷఴࣈΞΫηεʹ͍ͭͯ

  130. String.Index StringͷఴࣈΞΫηεʹ͍ͭͯ ࢝఺͔ΒͷΦϑηοτ

  131. ΞδΣϯμ - SwiftͷStringʹ͍ͭͯ - Unicodeʹ͍ͭͯ -Stringͱཁૉͷஔ͖׵͑ʹ͍ͭͯ

  132. ཁૉͷஔ͖׵͕͑Ͱ͖ͳ͍

  133. Array String " #

  134. Array String " # ఴࣈΞΫηεͰsetͰ͖Δ ఴࣈΞΫηεͰsetͰ͖ͳ͍

  135. Array String " # ఴࣈΞΫηεͰsetͰ͖Δ ఴࣈΞΫηεͰsetͰ͖ͳ͍ MutableCollectionʹ४ڌ͍ͯ͠Δ MutableCollectionʹ४ڌ͍ͯ͠ͳ͍

  136. Array String " # ఴࣈΞΫηεͰsetͰ͖Δ ఴࣈΞΫηεͰsetͰ͖ͳ͍ MutableCollectionʹ४ڌ͍ͯ͠Δ MutableCollectionʹ४ڌ͍ͯ͠ͳ͍

  137. Sequence Collection MutableCollection

  138. Sequence Collection MutableCollection - ఴࣈΞΫηεʹΑΔgetΛ ఆٛ - ఴࣈΞΫηεʹΑΔsetΛ ఆٛ

  139. Sequence Collection MutableCollection - ఴࣈΞΫηεʹΑΔgetΛ ఆٛ - ఴࣈΞΫηεʹΑΔsetΛ ఆٛ ܭࢉྔ͸O(1)

    ͲͪΒ΋ܭࢉྔ͸O(1)
  140. Sequence Collection MutableCollection String Sequence Collection MutableCollection Array

  141. Sequence Collection MutableCollection String Sequence Collection MutableCollection Array ఴࣈΞΫηεʹΑΔset ͕O(1)Ͱ࣮ݱͰ͖ͳ͍͔Β

  142. จࣈ ͏ a b c Unicode
 Scalar U+3046 U+61 U+62

    U+63 var string = “͏abc" string[string.startIndex] = "X"
  143. จࣈ ͏ a b c Unicode
 Scalar U+3046 U+61 U+62

    U+63 var string = “͏abc" string[string.startIndex] = "X"
  144. จࣈ X a b c Unicode
 Scalar U+58 U+61 U+62

    U+63 var string = “͏abc" string[string.startIndex] = "X"
  145. จࣈ ͏ a b c Unicode
 Scalar U+3046 U+61 U+62

    U+63 จࣈ X a b c Unicode
 Scalar U+58 U+61 U+62 U+63
  146. จࣈ ͏ a b c Unicode
 Scalar U+3046 U+61 U+62

    U+63 UTF-8 E3 81 85 61 62 63 จࣈ X a b c Unicode
 Scalar U+58 U+61 U+62 U+63 UTF-8 58 61 62 63
  147. จࣈ ͏ a b c Unicode
 Scalar U+3046 U+61 U+62

    U+63 UTF-8 E3 81 85 61 62 63 จࣈ X a b c Unicode
 Scalar U+58 U+61 U+62 U+63 UTF-8 58 61 62 63
  148. จࣈ ͏ a b c Unicode
 Scalar U+3046 U+61 U+62

    U+63 UTF-8 E3 81 85 61 62 63 όΠτ෯ 3 1 1 1 จࣈ X a b c Unicode
 Scalar U+58 U+61 U+62 U+63 UTF-8 58 61 62 63 όΠυ෯ 1 1 1 1
  149. None
  150. 1όΠτ

  151. a b c ͏

  152. a b c ͏ a b c X

  153. a b c ͏ a b c X

  154. a c b ͏ b c a X ೖΕସ͑ͨจࣈͷҎ߱ͷจࣈͷόΠτྻΛҠಈ͢Δඞཁ͕͋Δ ‑

    ఴࣈΞΫηεʹΑΔ set ͷܭࢉྔ͸O(N)
  155. ·ͱΊ - ҰͭͷCharacterͷ಺෦දݱ͕ͦΕͧΕҟͳΔͷͰɺ CharacterͷऔಘͱೖΕସ͑ͷܭࢉྔ͕O(1)ʹͳΒͳ͍ - IntͰఴࣈΞΫηεͰ͖ͳ͍ - MutableCollectionʹ४ڌ͠ͳ͍