Twitter Api Response:
"retweet_count" = 0;
retweeted = 0;
source = "<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>";
text = "ALTIN almU0131U015f baU015fU0131nU0131 gidiyor... bakalU0131m tU00fcrk lirasU0131 daha ne kadar deU011fersizleU015fecek @Turkiye @BorsaAltinU2026 https://twitter.com/i/web/status/1216306036602277889";
truncated = 1;
My code:
let request = client.urlRequest(withMethod: "GET", urlString: statusesShowEndpoint, parameters: params, error: &clientError)
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if connectionError != nil {
print("Error: (connectionError)")
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: [])
print("json: (json)")
} catch let jsonError as NSError {
print("json error: (jsonError.localizedDescription)")
}
}
}
how can I convert Unicode to string ? i have been don’t use model.
2
Answers
You can use u{Unicode}:
You can Use this Extension as well
I hope this will help.
I made a playground to test this issue. Swift will complain that the String contains incorrect escape characters.
This is a problem and the code will not compile:
Swift expect the Unicode to be formatted with ex ‘u0131’ or ‘u{0131} but you receive the twitter API with ‘U0131’
You need to “sanitise” the input first otherwise it will not work! Also when I tested the below I could not save the input in a string with the incorrect escaping. The compiler checks that the strings are correct before doing any operations on them.
I used a trick. Before saving the input from the file I split into an array of characters with map, of these characters I check with filter which one is an escaping backlash remove it and join the characters again to form a string.
Sorry but I did not find any other way, just having ‘U’ in my input would get Swift yelling at me.
What remains in the input string is “ALTIN almU0131U015f ..etc”
Now I need to replace those ‘U0131’ with ‘u0131’ and for this I use Regex:
And this is the final output of my String test as property of my struct after the conversion.
I apologise if my code is a bit messy but it was not easy to get past the string validation in Swift!
The below is the playground code in detail.
What I did is to create a json file with your input as a test:
Then create a struct reflecting the JSON properties, in this case only one: “test”