在 Unicode 转义符后的大括号中期望十六进制代码。

53

这在 Swift 的第一个测试版中可行。

var degree = "\u00B0" // degree symbol

现在我正在Xcode 6 Beta 5中遇到这个错误,但我不知道该做什么来纠正它。

Expected hexadecimal code in braces after unicode escape

2
专业提示:阅读每个Beta版本的发布说明。哦,错误信息也很好。 - zaph
3个回答

124

正确的代码是:

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

Xcode 6 beta 4 发布说明:

字符串字面量中的 \x、\u 和 \U 转义序列已合并成一个单一且错误率更低的 \u{123456} 语法。(17279286)


15
动态生成代码时有什么实现方法? - Bastien Beurier
@BastienBeurier 这里是我制作的一个将 Int 转换为表情符号的示例,希望能对你有所帮助。 - lochiwei
@BastienBeurier 修复生成器。 - Cœur

1
使用动态生成的。
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)
}

0
通过使用这些代码行,我们可以将十六进制文本转换为相应的符号。
 private func getDecodedUrl(url: String) -> String {
     let str: String = url.replacingOccurrences(of: "'\'", with: "\\")
     return str
 }

 let webUrl = NSMutableString( string: self.getDecodedUrl(url: convertedString) )
 CFStringTransform( webUrl, nil, "Any-Hex/Java" as NSString, true)
 print(webUrl)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接