这在 Swift 的第一个测试版中可行。
var degree = "\u00B0" // degree symbol
现在我正在Xcode 6 Beta 5中遇到这个错误,但我不知道该做什么来纠正它。
Expected hexadecimal code in braces after unicode escape
正确的代码是:
var degree = "\u{00B0}" // degree symbol
Xcode 6 beta 4 发布说明:
字符串字面量中的 \x、\u 和 \U 转义序列已合并成一个单一且错误率更低的 \u{123456} 语法。(17279286)
Int
转换为表情符号的示例,希望能对你有所帮助。 - lochiweiextension 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)
}
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)