NSDictionary objectForKey: string issue - ios

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.

Related

Core Data imports attributes (from type NSString) with spaces and new line

I am importing some data using NSXMLParser from XML into Core Data.
xml looks like:
<Translation>
<LanguageCode>EN</LanguageCode>
<SurahName>Al Anfal (The Spoils of War)</SurahName>
<TranslatedText>Believers are…</TranslatedText>
</Translation>
XML is ok i mean there aren't existing spaces there.
Then i want to display saved data on the App.
I concatenate different attributes into one string but it is not displayed in one line. (Integer values coming from other entity)
After debug, i realised that the attributes from type NSString added wrong into the core data. Namely they are containing spaces and line break.
I am using following code to concatenate string with integer values:
NSString *result = [NSString stringWithFormat:#"%# - :%i / %i",currentTranslation.surahName,surahNr,verseNr];
Result should be a string without line breaks, but surahName pushes following integers to the next line.
Result:
Al Anfal (The Spoils of War)
- :8 / 2
As you see this part "- :8 / 2" printed in new line which pushed by surahName.
I searched this problem but i didn't find something. I don't know what i am doing wrong.
I hope the description above was clear.
Thank you in advance
The problem should be in the way you parse your XML.
In the exemple you give, there is a new line caracter after the ending of each element.
Here is the delegate's methods called to parse your surahname line:
didStartElement (<SurahName>)
foundCharacters (Al Anfal (The Spoils of War))
didEndElement (</SurahName>)
foundCharacters (new line caracter)
In foundCaracters method you have to check if those caracters are in an opened element, and if this element is supposed to contain caracters.
If you determine caracters as usefull, add it to your current content, else don't use it.

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

String with unicode and UILabel with multiline

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.

Showing unicode in the console in the right format

NSSet *subFolders = [_account subscribedFolders];
NSLog(#"subFolders: %#",subFolders);
Output:
...
"[Gmail]/\U05d8\U05d9\U05d5\U05d8\U05d5\U05ea",
"[Gmail]/\U05d7\U05e9\U05d5\U05d1"
...
Is there any way I can show the above text in its original language (Hebrew) ?
Things I tried:
changing the debugger from LLDB to GDB - Didn't work
Checking under preferences -> Text Editing UTF-* is selected
Thanks
There is no issue with displaying unicode characters in the console, so I would assume it's the way the string is getting into the set in the first place.
I would suggest iterating over all the objects inside subFolders with something like:
for( id object in [subFolders allObjects] ) {
//Print out the name of the item explicitly
}
Even if this doesn't work, it at least lets you work with the strings directly. If it's still printing out:
"[Gmail]/\U05d8\U05d9\U05d5\U05d8\U05d5\U05ea"
It would look as if you're being sent escaped unicode characters, and I would suggest this: https://stackoverflow.com/a/7861345/352891 - this may work directly on NSSet's description
NSString* strOld=[NSString stringWithFormat:#"%#",responseObject];
NSLog(#"%#",[NSString
stringWithCString:[strOld cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding]);

NSString to NSDictionary

I have a string (from HTTP Header) and want to split it into a dictionary.
foo = \"bar\",baz=\"fooz\", beta= \"gamma\"
I ca not guarantee that the string is the same every time. Maybe there are spaces, maybe not, sometimes the double quotes are escaped, sometimes not.
So I found the solution in PHP with regular expressions. Unfortunately I can't convert it to work on iOS.
preg_match_all('#('.$key.')=(?:([\'"])([^\2]+?)\2|([^\s,]+))#', $input, $hits, PREG_SET_ORDER);
foreach ($hits as $hit) {
$data[hit[1]] = $hit[3] ? $hit[3] : $hit[4];
}
Can anybody help me converting this to Objective-C?
I met a guy which is kinda RegEx guru. He explained the whole stuff and I got the following (working!!!!) solution in RegEx.
This gives me strings like foo="bar":
(?<=[,\\s])((realm|qop|nonce|opaque)=(?:([\"'])([^\2]+?)\2|([^\\s,]+)))
I then use another RegEx to split it by key and value to create a dictionary.

Resources