Why Can't You Loop Over Ranges of Characters in Swift

This is a follow up from my previous post: The power and expressiveness of Swift ranges. For a Character Range: contain works fine let numericalRange = 1...10 numericalRange.contains(8) // true let a: Character = "a" let z: Character = "z" let alphabeticalRange = a...z alphabeticalRange.contains("k") // true for-loop and count don’t work print(numericalRange.count) // 10 print(alphabeticalRange.count) // ❌ Referencing property 'count' on 'ClosedRange' requires that 'Character' conform to 'Strideable' for num in numericalRange { print(num) // 1 2 3 4 5 6 7 8 9 10 } for char in alphabeticalRange { // ❌ Referencing instance method 'next()' on 'ClosedRange' requires that 'Character' conform to 'Strideable' print(char) } What both of those errors mean is that Swift can’t figure out what the next character is for a given character....

November 30, 2024 Β· 6 min