countelements

Swift countElements() return incorrect value when count flag emoji

夙愿已清 提交于 2019-12-10 16:17:16
问题 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)) // ["🇩🇪", "🇩🇪", "🇩🇪", "🇩🇪",

Swift countElements() return incorrect value when count flag emoji

梦想与她 提交于 2019-11-27 01:37:12
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. 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)) // ["🇩🇪", "🇩🇪", "🇩🇪", "🇩🇪", "🇩🇪"] Also String is a collection of its characters (again), so one can obtain the character count with str1

Swift countElements() return incorrect value when count flag emoji

a 夏天 提交于 2019-11-26 08:24:08
问题 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)) // ["🇩🇪", "🇩🇪", "🇩🇪",