Swift countElements() return incorrect value when count flag emoji
问题 let str1 = "🇩🇪🇩🇪🇩🇪🇩🇪🇩🇪" let str2 = "🇩🇪.🇩🇪.🇩🇪.🇩🇪.🇩🇪." println("\(countElements(str1)), \(countElements(str2))") Result: 1, 10 But should not str1 have 5 elements? The bug seems only occurred when I use the flag emoji. 回答1: Update for Swift 4 (Xcode 9) As of Swift 4 (tested with Xcode 9 beta) grapheme clusters break after every second regional indicator symbol, as mandated by the Unicode 9 standard: let str1 = "🇩🇪🇩🇪🇩🇪🇩🇪🇩🇪" print(str1.count) // 5 print(Array(str1)) // ["🇩🇪", "🇩🇪", "🇩🇪", "🇩🇪",