How to convert an Int to a Character in Swift(如何在 Swift 中将 Int 转换为字符)
问题描述
我在这里挣扎和失败了十多分钟,我屈服了.我需要在 Swift 中将 Int 转换为 Character,但无法解决.
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.
问题
你如何将一个 Int (integer) 转换 (cast) 为一个 Character (char) 在 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! :)
推荐答案
不能将整数直接转换为 Character 实例,但可以从整数转换为 UnicodeScalar 到 Character 并再次返回:
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)))
}
这篇关于如何在 Swift 中将 Int 转换为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Swift 中将 Int 转换为字符
基础教程推荐
- 我的 UIImageView 的任务 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
