Decode unicode string in iOS - ios

I got an app, where i download data through JSON. But when i am trying to show NSStrings i see something like this:
\u041e\u041d\u0410!!! etc.
How can I decode it into normal symbols?
in our team for this problem we create our own decoder

You'll see \u041e\u041d\u0410!!! only in NSLog's console view.. just assign NSSTring's value to some UILabel or UITextField it will show properly

If you log just for example nsdictioanry you will see unicode \u041e\u041d\u0410, but if you will log NSString (objectForKey:#"key") it will be ok.

Related

Post NSAttributedString to server via REST API

I want to send the attributedString to the server along with all attribute information and then receive this on another device and display the attributedString in UITextView again. I have created a Transformable attribute in CoreData with NSAttributedString as custom class. i get the UITextView attributedString and save it in CoreData and later on retrieve it and display back on UITextView. This all works fine, however my use case requires me to send the formatted text along with all attributes information to server using REST API. I tried following solutions but none of them worked:
Convert NSAttributedString to String (Problem: it removes the formatting information)
serialise NSAttributedString object using NSKeyedArchiver.archivedData and then send the resultant Data to server (Problem: I get error in POST method)
serialise NSAttributedString and convert the resultant Data to string using testString.data(using: String.Encoding.utf8) (Problem: When i try to convert this string back to Data i get error)
Can anybody guide me in this, if i am approaching this problem the right way or not? I am open to upgrading to latest Swift version if there is any new feature which i can use to solve this problem. Any kind of help is appreciated.
Thank you
Affan
Convert NSAttributedString to HTML.
I think solved your problem.
How do i convert NSAttributedString into HTML string?
Display HTML data in a label or textView

Encoding problems while extracting NSDictionary

I have a JSON file with texts and titles. Titles are in English language, but texts sometimes include cyrillic symbols. When I extract JSON to NSDictionary everything seems to be good, because log shows me english and UTF symbols:
\U00c8\U00f1\U00f2\U00ee\U00f0\U00e8\U00df about...
But when I try to get a string from NSDictionary with this value it gives me strange symbols:
Èñòîðèß about...
It seems like there is everything allright before I try to extract any specific value from the dictionary. So I need help to understand how to get the same exact (English and UTF symbols) value which shows me when I NSLog the whole dictionary.
This is how I get the value now: NSLog(#"text: %#", dict[#"results"][0][#"text"]);
It seems, you haven't encoded your data properly. See your json file in UTF-8:
That's why you can not decode it into normal NSString

Obtain currency symbol unicode character from JSON response to present in UILabel

How to store Unicode character like 'EURO SIGN' (U+20AC) in MySQL db and than transfer it to ios application in JSON for showing it in UILabel, for example.
Just hardcoded NSString like #"\u20AC" for local application needs works great.
But storing \u20AC in db leads to \u20AC in result after transferring. Other manipulations with received value has no effect.
In short - how to store currency codes on server side and transfer them to ios application via JSON?
If you are using PHP, use the second argument thus:
json_encode($s, JSON_UNESCAPED_UNICODE);
to avoid getting the \u codes.

Escape bad characters for iOS UILabel?

I'm feeding some NSString data (forum posts queried from my website) into a UILabel as part of my first app. Thing is, sometimes, depending on the content of the post, the Label goes entirely blank. I've tinkered enough to discern that there are certain characters that cause the problem, but I can't quite pin down the full set.
Is there a collected list of character types to watch out for with this kind of thing? And even better, is there a method for escaping them, or automatically converting them into something more acceptable?
Thank you for helping out a n00b!
Looks like you have whitespace or new line in your string:
Try this:
NSString* labelText = [stringFromWebsite stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
I am trying to give you some clue even you haven't put piece of code here in your question.
You should firstly try to encode that coming String by using appropriate method like encoding:NSUTF8StringEncoding and still and try to set decoded string to your UIlabel.
Another alternative is that you can create some regex for filtering purpose of that coming string. you can find many of similar thread over the google.

Formatting NSString for superscript and subscript

I am writing a utility app for some coworkers. The app is essentially a custom notepad, with buttons that represent the shorthand they use to transcribe a task. All of the buttons add a string to arrays that I have set up to hold the transcript, and I add the strings to the row arrays like this.
[currentRow addObject:#"("];
Some of the shorthand needs to be written in subscript, and some in superscript. There are not Unicode characters for all of the characters that I need, so I have been trying to sort through the code around Attributed Strings,but I'm not quite getting it. Does anyone have advice on this or some sample code?
Also, after this transcript is printed to the screen during transcription, I send it to an email message body.. so I assume I'll need to worry about formatting there as well. I am currently using plain text, but the email could be HTML. Thanks!
If you display the text in a WebView you can use html tags to set superscript. It also has the advantage to run on older iOS versions and you can reuse the text in your mail.
NSString *myText=#"This text contains <sub>subscript</sub> and <sup>superscript</sup> text.";
[self.myWebView loadHTMLString:myText baseURL:nil];

Resources