Converting to Char/String from Ascii Int in Swift

后端 未结 3 1350
萌比男神i
萌比男神i 2021-01-17 17:22

I\'m trying to convert the integer representation of an ascii character back into a string.

string += (char) int;

In other languages like J

3条回答
  •  一生所求
    2021-01-17 17:57

    Here's a production-ready solution in Swift 3:

    extension String {
        init(unicodeScalar: UnicodeScalar) {
            self.init(Character(unicodeScalar))
        }
    
    
        init?(unicodeCodepoint: Int) {
            if let unicodeScalar = UnicodeScalar(unicodeCodepoint) {
                self.init(unicodeScalar: unicodeScalar)
            } else {
                return nil
            }
        }
    
    
        static func +(lhs: String, rhs: Int) -> String {
            return lhs + String(unicodeCodepoint: rhs)!
        }
    
    
        static func +=(lhs: inout String, rhs: Int) {
            lhs = lhs + rhs
        }
    }
    

    Usage:

    let a = String(unicodeCodepoint: 42) // "*"
    var b = a + 126 // "*~"
    b += 33 // "*~!"
    

    Note that this works with all ASCII and Unicode codepoints, so you can do this:

    var emoji = String(unicodeCodepoint: 0x1F469)! // "

提交回复
热议问题