Expected hexadecimal code in braces after unicode escape

The correct code is:

var degree = "\u{00B0}" // degree symbol

From the Xcode 6 beta 4 release notes:

The \x, \u and \U escape sequences in string literals have been consolidated into a single and less error prone \u{123456} syntax. (17279286)


to use a dynamically generated

extension String {
    func hexToDecimal() -> String {
        var myValue = self
        if self.hasPrefix("0x") || self.hasPrefix("0X") {
            let start = self.index(self.startIndex, offsetBy: 2)
            myValue = String(self[start...])
        }

        var sum = 0
        for num in myValue.uppercased().utf8 {
            sum = sum * 16 + Int(num) - 48 
            if num >= 65 {
                sum -= 7
            }
        }
        return String(sum)
    }
}

let unicodeValue = "80" // decimal

// or 

let unicodeValue = "0x50".hexToDecimal()

if let uInt8 = UInt8(unicodeValue) {
    let scalar = UnicodeScalar(uInt8)
    let str = String(scalar)
}

Tags:

Swift

Xcode6