Expected Hexadecimal Code in Braces After Unicode Escape

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)

I receive an improperly formatted unicode in a String

You may want to give us more context regarding what the raw server payload looked like, and show us how you're displaying the string. Some ways of examining strings in the debugger (or if you're looking at raw JSON) will show you escape strings, but if you use the string in the app, you'll see the actual Unicode character.

I wonder if you're just looking at raw JSON.
For example, I passed the JSON, {"foo": "Eat pok\u00e9."} to the following code:

let jsonString = String(data: data, encoding: NSUTF8StringEncoding)!
print(jsonString)
let dictionary = try! NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: String]
print(dictionary["foo"]!)

And it output:


{"foo": "Eat pok\u00e9."}
Eat poké.

By the way, this standard JSON escape syntax should not be confused with Swift's string literal escape syntax, in which the hex sequence must be wrapped in braces:

print("Eat pok\u{00e9}.")

Swift uses a different escape syntax in their string literals, and it should not be confused with that employed by formats like JSON.

Swift JSON Decodable \u0000

You need to escape \u0000 characters first - this can be done before decoding:

guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
}

let escaped = Data(String(data: data, encoding: .utf8)!.replacingOccurrences(of: "\0", with: "").utf8)
...
return try decoder.decode(T.self, from: escaped)

Note: force-unwrapping for simplicity only.


In Playground you can escape it with an additional \ (to make it work):

let json = """
{
"name": "Durian \\u0000"
}
""".data(using: .utf8)!

or replace it with \0 (to make it fail - behave like during the decoding):

let json = """
{
"name": "Durian \0"
}
""".data(using: .utf8)!

Incomplete universal character name in UI Testing

You can use the following workaround as this seems to be a bug in xcode:

replace all \U to \u
and it should work.

Zero Width Space in Swift?

Use this syntax in Swift

let emptyString = "\u{200B}"

Of course you can also use unicode characters directly in your Swift code.

low speed working with swiftyJSON

I would strongly recommend ditching SwiftyJSON and the string-and-dictionary based searching you're doing, and replacing this with much simpler code based on Codable.

struct Sefaresh: Codable {
let contacts: [Contact]
}

struct Contact: Codable {
let code, name, id, carton, tedad, litr, vahed, vazn: String
}

let sefaresh = try? JSONDecoder().decode(Sefaresh.self, from: jsonData)

your first loop then becomes

for (index, contact) in sefaresh.contacts.enumerated() {
if contact.stringValue.range(of: txtSearch.text!) != nil {
arrayIndexPathSearch.append(index)
}
}

and the second is simply

for contact in sefaresh.contacts {
arraySearch.append(contact.name)
}

Convert unicode symbols \uXXXX in String to Character in Swift

You can use \u{my_unicode}:

print("Ain\u{2019}t this a beautiful day")
/* Prints "Ain’t this a beautiful day"

From the Language Guide - Strings and Characters - Unicode:

String literals can include the following special characters:

...

  • An arbitrary Unicode scalar, written as \u{n}, where n is a 1–8 digit
    hexadecimal number with a value equal to a valid Unicode code point

How to decode unicode characters in swift 3?

The easiest way is to use CFStringTransform like below.

let wI = NSMutableString( string: "\\u263a" )
CFStringTransform( wI, nil, "Any-Hex/Java" as NSString, true )
someUILabel.text = wI as String


Related Topics



Leave a reply



Submit