unicode转义后括号中的预期十六进制代码

时间:2014-08-10 02:50:16

标签: swift xcode6

这是Swift的第一个测试版。

var degree = "\u00B0" // degree symbol

现在我收到了这个错误,我不明白在Xcode 6 Beta 5中我需要做些什么来纠正它。

Expected hexadecimal code in braces after unicode escape

2 个答案:

答案 0 :(得分:82)

正确的代码是:

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

从Xcode 6 beta 4发行说明:

  

字符串文字中的\ x,\ u和\ U转义序列    已被合并为一个单一的    并且更不容易出错。\ n \ n语法。 (17279286)

答案 1 :(得分:0)

使用动态生成的

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)
}