Iterate through alphabet in Swift

前端 未结 8 1891
暗喜
暗喜 2020-12-30 02:23

in Obj-C it was possible to iterate through alphabet with:

for (char x=\'A\'; x<=\'Z\'; x++) {

In Swift this isn\'t possible. Any idea h

8条回答
  •  时光说笑
    2020-12-30 02:41

    Updated for Swift 3.0 and higher

    let startChar = Unicode.Scalar("A").value
    let endChar = Unicode.Scalar("Z").value
    
    for alphabet in startChar...endChar {
    
        if let char = Unicode.Scalar(alphabet) {
    
            print(char)
        }
    }
    

提交回复
热议问题