如何将Int转换为Swift中的字符 [英] How to convert an Int to a Character in Swift

查看:169
本文介绍了如何将Int转换为Swift中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里挣扎和失败超过十分钟,我给了。我需要将一个Int转换为一个字符在Swift,不能解决它。

I've struggled and failed for over ten minutes here and I give in. I need to convert an Int to a Character in Swift and cannot solve it.

问题

如何转换( cast 字符 char )将 em>)in Swift?

How do you convert (cast) an Int (integer) to a Character (char) in Swift?

说明性问题/任务挑战

生成for循环打印字母'A'到'Z',例如像这样:

Generate a for loop which prints the letters 'A' through 'Z', e.g. something like this:

    for(var i:Int=0;i<26;i++) {      //Important to note - I know 
        print(Character('A' + i));   //this is horrendous syntax...
    }                                //just trying to illustrate! :)


推荐答案

a 字符实例,但可以从整数到 UnicodeScalar 字符并返回:

You can't convert an integer directly to a Character instance, but you can go from integer to UnicodeScalar to Character and back again:

let startingValue = Int(("A" as UnicodeScalar).value) // 65
for i in 0 ..< 26 {
    print(Character(UnicodeScalar(i + startingValue)))
}

这篇关于如何将Int转换为Swift中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆