Help us understand the problem. What is going on with this article?


More than 3 years have passed since last update.

[swift] String.CharacterViewが使いづらいと思う、そんなあなたにArrayのおすすめ - Qiitaを拝読して気になることがあったので調べてみました。

let w = "\u{65}\u{301}"                 // "é" == e + アクセントマーク
let wcv = w.characters
let wcv_endIndex = wcv.endIndex         // 2

let wcv_a = Array(wcv)                  // ["é"]
let wcv_a_endIndex = wcv_a.endIndex     // 1

let wcv_c0 = wcv[wcv.index(wcv.startIndex, offsetBy: 0)]    // "é"
let wcv_c1 = wcv[wcv.index(wcv.startIndex, offsetBy: 1)]    // fatal error: Can't form a Character from an empty String

"é"のcharactersのendIndexは2ですがArrayにしたら1になりました。しかし文字を取得しようとするとfatal errorになりました。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away