如何在SWIFT中将Int转换为字符

p8h8hvxi  于 2022-10-04  发布在  Swift
关注(0)|答案(9)|浏览(466)

我在这里挣扎了十多分钟,失败了,我投降了。我需要将Int转换为SWIFT中的字符,但无法解决。

问题

如何在SWIFT中将Int(INTEGER)转换(CAST)为Character(char)?

说明性问题/任务挑战

生成一个for循环,该循环打印字母‘A’到‘Z’,例如:

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! :)
ikfrs5lh

ikfrs5lh1#

不能将整数直接转换为Character示例,但可以从整型到UnicodeScalar再到Character,然后再转换回来:

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

vc6uscn92#

尝尝这个

for i in 0...25
{
    let string = String(format: "%c", i+65) as String
    NSLog("%@", string)
}
9avjhtql

9avjhtql3#

如何在SWIFT中将Int转换为字符

  • 为方便将来的访客,我只提供题目的基本答案,而非问题本身的细节。*

这是一个分两步走的过程。将Int转换为UnicodeScalar,然后将UnicodeScalar转换为Character

let myInteger: Int = 97

// convert Int to a valid UnicodeScalar
guard let myUnicodeScalar = UnicodeScalar(myInteger) else {
    return
}

// convert UnicodeScalar to Character
let myCharacter = Character(myUnicodeScalar)

// results
print(myCharacter) // a

(source)

或者或者..。

if let myUnicodeScalar = UnicodeScalar(97) 
    let myCharacter = Character(myUnicodeScalar)
}

另见

rwqw0loc

rwqw0loc4#

到目前为止,我想出了这个:

for i in 0 ..< 26 {
    print(Character(UnicodeScalar(Int(UnicodeScalar("A").value) + i)))
}

如果你只是想把“A”变成“Z”,你可以避开数学运算,直接这么做:

for c in UnicodeScalar("A").value...UnicodeScalar("Z").value {
    print(String(UnicodeScalar(c)))
}
68de4m5k

68de4m5k5#

要获得有用的背景信息,请使用Vacawama和Nate Cook的UnicodeScalar-

let startingValue = Int(UnicodeScalar("A").value)
 for i in 0..<26 {
    let itemStr = String(UnicodeScalar(i + startingValue))

    items.append("Item " + itemStr)
}
ca1c2owp

ca1c2owp6#

只需将整数转换为字符串,然后将字符串转换为字符

let number = 5
let numChar = Character(String(number))
nmpmafwu

nmpmafwu7#

以下是Int的一个扩展,它提供了一个对应的Letter函数:

extension Int {
    func correspondingLetter(inUppercase uppercase: Bool = false) -> String? {
        let firstLetter = uppercase ? "A" : "a"
        let startingValue = Int(UnicodeScalar(firstLetter)!.value)
        if let scalar = UnicodeScalar(self + startingValue) {
            return String(scalar)
        }
        return nil
    }
}

请注意,如果int大于26,您将获得特殊字符。

busg9geu

busg9geu8#

新的和更新的

for charac in Unicode.Scalar("A").value...Unicode.Scalar("Z").value {
    print(Unicode.Scalar(charac)!, terminator:" ")}

打印:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

感谢@Vacawama的帮助,我喜欢这个版本,特别是对于Swift(5),因为:

for charac in Unicode.Scalar("a").value...Unicode.Scalar("z").value {
    print(Unicode.Scalar(charac)!, terminator:" ")}

打印:

a b c d e f g h i j k l m n o p q r s t u v w x y z

而且不需要抬头,即使我们应该知道我们的Unicode?哈哈等等..。

atmip9wb

atmip9wb9#

//Current swift version is 5.7

//Prints character of unicode scaler 65
let num = 65
if let character = UnicodeScalar(num) {
    print(character) // prints A
}

//Similarly looping over 'a...z' 
let numRange = 97...122
for num in numRange {
    print("(num) = (UnicodeScalar(num)!)")
}

相关问题