Showing unicode in the console in the right format - ios

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]);

Related

Objective-C iOS parse string according to the definition

I want create an iOS app for my school.
This App will show Week schledule and when I tap on Cell with Subject, it will show me detail info about subject...
My problem:
Our teachers use shotcuts for their names, but I want show their full name... I created the file "ucitele.h" with definitions of their names, but I don't know, how to use it 😕.
This is how that file looks:
//
// ucitele.h
//
#define Li #"RNDr. Dan---vá"
#define He #"Mgr. Ja---hl"
#define Sm #"Ing. Mich---rek"
#define Ks #"Mgr. Svat---á"
I get the shortcut of Teacher from previous view from "self.ucitel" and I maybe want compare the contents of the "self.ucitel" with definitions and set the "ucitelFull" string from the definitions? I don't know how to say it 😕.
when the content of the self.ucitel will be #"Sm", than I want parse "ucitelFull" as #"Ing. Mich---rek"
Answers in Objective-C only please
Okay, sounds like your trying to map a short identifier to a full name:
-(NSString*)fullNameFromShortName:(NSString*)short {
NSDictionary * names = #{#"Li" : #"RNDr. Dan---vá",
#"He" : #"Mgr. Ja---hl", ... };
return [names objectForKey:short];
}
Use like:
self.ucitelFull = [self fullNameFromShortName:self.ucitel];
This is a dictionary that has the short name as a key and the full name as the value.
Some further suggestions:
try using lowercase keys and comparing lowercaseString's, incase the user doesn't enter the value with the correct case.
You can move the dictionary definition into a json file and read it from your bundle, to eliminate the hardcoding

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.

Getting an XML value from an NSArray

I'm currently using SMXMLDocument as my parser and so far it does a fantastic job parsing some XML files. The only problem that I have encountered is that it cannot seem to handle children with the same name, well at least in my case. But this parser can return the parsed XML as an NSArray.
The NSArray would look like this:
(
"<id>https://spreadsheets.goog\U2026</id>",
"<updated>2013-12-23T17:54:04.814Z</updated>",
"<category term=\"http://schemas.google.com/spreadsheets/2006#cell\" scheme=\"http://schemas.google.com/spreadsheets/2006\"/>",
"<title type=\"text\">A1</title>",
"<content type=\"text\">What?</content>",
"<link rel=\"self\" type=\"application/atom+xml\" href=\"https://spreadsheets.google.com/feeds/cells/some key/od6/private/full/R1C1\"/>",
"<link rel=\"edit\" type=\"application/atom+xml\" href=\"https://spreadsheets.google.com/feeds/cells/some key/18o84x\"/>",
"<cell row=\"1\" col=\"1\" inputValue=\"What?\">What?</cell>",
"<id>A1</id>",
"<status code=\"200\" reason=\"Success\"/>",
"<operation type=\"update\"/>")
So my question is, how would I get the values (and attributes) from the XML? If there is a way to tokenize this (ie going through the array as an NSString with a for-in loop or something) without having to use a big fancy library that would be great. Thanks in advance.
Update:
Here is the NSLog of what happens if I try to get id with SMXMLDocument:
Code:
SMXMLElement* testEntry = [feed childNamed:#"entry"];
NSLog(#"id: %#", [testEntry valueWithPath:#"id"]);
Output:
id: https://spreadsheets.google.com/feeds/cells/some key/od6/private/full/R1C1
After hours of battling with the code, I ended up using another parser (as a secondary) called SHXMLParser because of it's neat syntax. It is capable returning multiple values from nodes with the same name as an NSArray. From there I just compared the contents in the array and picked the one I wanted.

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.

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