Expected hexadecimal code in braces after unicode escape
Asked Answered
B

3

54

This worked in the first beta of Swift.

var degree = "\u00B0" // degree symbol

Now I'm getting this error and I don't understand what I need to do to correct it in Xcode 6 Beta 5.

Expected hexadecimal code in braces after unicode escape
Batiste answered 10/8, 2014 at 2:50 Comment(1)
Pro Tip: Read the release notes for each Beta release. Oh, the error message was a good one too.Oxfordshire
B
125

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)

Burdelle answered 10/8, 2014 at 2:57 Comment(3)
Any idea how to do when code is dynamically generated?Whitsun
@BastienBeurier here is an example I made on converting Int to emojis, hope that helps.Communist
@BastienBeurier fix the generatorZoophilia
C
1

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)
}
Charitacharitable answered 28/5, 2020 at 2:59 Comment(0)
G
0

By using these lines of code we can convert hexadecimal text to the according symbol

 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)
Graecize answered 22/2, 2023 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.