String with unicode and UILabel with multiline - ios

I'm using XMLDictionary for save data from xml to NSDictionary and then I print NSDictionary I saw some code in unicode:
"text" = "Test \U2013 test\\nTest \U2013 test";
So when I use UILabel with multiline it print test - test\nTest - test
but I expected for:
test - test
Test - test
How I can make this?

Reading in xml characters can be interesting. XML doesn't know that \ means "Special character comes next". It knows that "\" means "I want you to write a "\".
So to get what you are looking for you are going to have to read in your string from the dictionary, then do a [string stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"]; on it to get your carriage returns back.
You can save carriage returns in xml, but not using the "\" method.

Related

Is there any way to replace double quotes with a backslash in swift

I have a submit form where there are multiple textfields.
Whenever user enters text like "Hi, my name is "xyz"", the service does not accept this JSON due to double quotes in my string.
Please suggest ways to escape this character.
I have tried using encode and decode JSON, replaceOccurrencesOf methods, but none work.
replaceOccurrencesOf()
The below code snippet with replace "(double quote) in a string by \". This will help to replace "(double quote) by any string or character in a given string.
Swift 5 or above
let replacedString = stringToBeModified.replacingOccurrences(of: "\"", with: #"\""#)
Instead of putting the name (i.e., "XYZ" if you getting xyz from textfield ) why not to place (textField.text!) it will not put extra " "

Convert UTF string to Chinese Language from JSON response

I tried everything to convert JSON response to Chinese language but not getting any success. I need to display those string in uilabel.
This is the response I'm getting:
sentence = "\U00e6\U201a\U00a8\U00e5\U00a5\U00bd\U00e3\U20ac\U201a";
pinyin = "n\U00c3\U00adn h\U00c4\U0192o"
Converting sentence's string should be like 您好 but I'm getting 您好。
For pinyin I'm getting exactly right string [[nín hăo]] in label without converting but for sentence it gives me wrong value.
I'm using XCode 7.1 and my deployment target is 8.0.
Hi thanks all for helping and trying :) i ended up solving my own problem. What I did is directly put dict value to label text rather than passing from NSString. Taking it into string will give me value like 您好。
Here is what i've done.
cell.lblWord.text = [NSString stringWithFormat:#"Word: %#",[[dic objectForKey:#"cat"]objectForKey:#"chart"]];
It's strange but true, tried before but wasn't working.

Convert Unicode Emoji to NSString (Not \ue415 format)

I am trying to convert an emoji to an NSString. I previously asked a question on how to do the opposite (convert an NSString to a unicode) at NSString to Emoji Unicode. I thought perhaps it would be best to ask this as a new question here.
How can I convert an NSString containing an emoji (😃) to an NSString containing a unicode in this format (U0001F603)?
This question is basically the reverse engineering of the solution from the previous page. The catch is the project does not use the \ue415 format, but rather the U0001F603 format.
Edited per comment:
2014-07-11 11:37:19.448 emoticon[******] unicode: 😂
unicode = [NSString stringWithFormat:#"%#\\UFE0E", unicode];
2014-07-11 11:37:19.449 emoticon[******] unicode: 😂\UFE0E
SECOND COMMENT RESPONSE
I'm not entirely sure if I follow what you mean by I didn't add the first line of code. I hope I haven't been unclear. To try and be more specific on what I would like, I logged your code in, and then logged what I wish to get:
NSString *first = #"😃";
NSString *second = #"😃\\UFE0E";
NSString *third = #"U0001F603\\UFE0E";
2014-07-11 12:00:45.815 emoticon[******] first: 😃, second: 😃\UFE0E, third: U0001F603\UFE0E
2014-07-11 12:00:45.816 emoticon[******] desiredString: U0001F603
My hope is to produce the desiredString by converting the emoji to the desired string.
THIRD COMMENT RESPONSE
What you need is using the escape character \U0000FE0E to the end of all Unicode characters to make it skip the emoji and display the proper Unicode character.
Here's the code:
#"😃" //This shows the colorful emoji icon.
#"😃\U0000FE0E" //This shows the good old Unicode character.
You can also add it to the character code:
#"U0001F603\U0000FE0E"
Here is a "pseudo-code" (JavaScript, you can run it in your browser's console) ..for opposite-direction.
String.fromCharCode(
((0x1F603 - 0x10000) >> 10) | 0xD800
,
((0x1F603 - 0x10000) % 0x400) | 0xDC00
)
=>>"😃"
Just reverse the bytewise operations, and zero-pad it.
If you are a programmer it should be more than easy for you.
..give a man a fish...
source: JavaScript Ninja - Easy Unicode Emoji Generator 😁🌠🐬

Excessive inter-word spacing when parsing XML

I am parsing XML using NSXMLParser. Everything works good except one XML tag:
<place>USA , Boston</place>
when I parse this tag, the value is
USA , Boston
Somehow spaces are added between words. Any ideas why it is happening and how can I fix it?
UPDATED
The code I am using is straightforward. The string that I receive in parser:foundCharacters: delegate call is already with spaces. I am using:
[_currentString appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
to remove spaces and new lines, but it is only for the beginning and the end of the string.
In FoundCharacters method we have to replace these special character.
string =[string stringByReplacingOccurrencesOfString:#"\t"
withString:#""];
[_currentString appendString:string];
Will be good if you show you code of in short if you can edit the XML format than one solution will be you can add the tag value between CDATA like
<place><![CDATA[USA , Boston]]></place>
CDATA section is a section of element content that is marked for the parser to interpret as only character data

NSDictionary objectForKey: string issue

I have a NSDictionary created with data from a web api.
Here is the dictionary logged:
{
chapter = {
text = "\n \tAmo\U00cc\U0081s";
};
}
When loging [dict objectForKey:#"chapter"] looks like this:
{
text = "\n \tAmo\U00cc\U0081s";
}
And when logging [dict objectForKey:#"text"] I get
AmoÌs
which is not correct, it should be Amo\U00cc\U0081s / Amós
It seems to be an encoding problem, but I can't figure it out.
Any idea why this is happening?
Thanks
The NSLog is printing correctly!!!
You can not print Unicode text to log. You have new line, a tab and \U00cc and \U0081 which is converting to some un-readalbe texts.
This is not a bug. CF and Cocoa interpret %S and %C differently from how printf and its cousins interpret them. CF and Cocoa treat the character(s) as UTF-16, whereas printf (presumably) treats them as UTF-32.
The CF/Cocoa interpretation is more useful when working with Core Services, as some APIs (such as the File Manager) will hand you text as an array of UniChars, not a CFString; as long as you null-terminate that array, you can use it with %S to print the string.
Copied from here.

Resources