NSString componentsSeparatedByString "&" but ignore "& amp;" - ios

Need to separate a string on & but not on &.
Is there a more elegant way to code this up rather than just replacing it first then separating it on the &? Like this...
query = [query stringByReplacingOccurrencesOfString:#"&" withString:#"~~~"];
NSArray * kvpairs = [query componentsSeparatedByString:#"&"];
NSMutableArray *mArr = [[NSMutableArray alloc] init];
for (NSString *kvp in kvpairs) {
[mArr addObject:[kvp stringByReplacingOccurrencesOfString:#"~~~" withString:#"&"]];
}
kvpairs = [NSArray arrayWithArray:mArr];
[mArr release];

You could use NSRegularExpression to enumerate through the string on a regular expression that matches & but not &, e.g.: #"&(?!amp;)". This will be more cumbersome than your current method but more exact, because it will work without modifying the original string and doesn't rely on a token value.
If you control the input to this method and can guarantee that ~~~ won't appear normally, there's nothing wrong with using ~~~. However if you don't control the input then you should attempt to parse the string without modification.

There isn't really anything wrong with that, as long as you are 100% sure that the string ~~~ won't occur in your query.
If you are not, my next step would be to implement a method to parse the string into an array. In this method, you could find each &, then check if it is followed by amp;. If it is, move on to the next one, if it is not, cut the string there and repeat.

HTML entities and references are really not a good thing to have hanging around in URLs. Unless there is a very good reason that HTML entities must be used, I would recommend using '%26' in the URL encode an ampersand.
That being said, you will run into problems if '&' is not the only HTML entity or reference, so unless you are absolutely sure '&' is the only one, you will need a more robust solution.

Related

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.

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

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.

unexpected '#' in program

I am a newbie on iOS development, so I find no clue when error like this, the code is like:
- (void)postToWall {
FBStreamDialog *dialog = [[[FBStreamDialog alloc] init]
autorelease];
dialog.userMessagePrompt = #"Enter your message:";
dialog.attachment = [NSString
stringWithFormat:#"{\"name\":\"Facebook Connect for
iPhone\",\"href\":\"http://developers.facebook.com/
connect.phptab=iphone\",\"caption\":\"Caption\",
\"description\":\"Description\",\"media\":[{\"type\":
\"image\",\"src\":\"http://img40.yfrog.com/img40/
5914/iphoneconnectbtn.jpg\",\"href\":
\"http://developers.facebook.com/connect.php?
tab=iphone/\"}],\"properties\":{\"another link\":
{\"text\":\"Facebook home page\",\"href\":
\"http://www.facebook.com\"}}}"];
[dialog show];
}
I am trying to learn from a online tutorial about facebook connect, so I got this error in the code and the file includes:
import "FBSession.h"
import "FBLoginButton.h"
Do you think it could be this causes the problem?
Either write the string in a single line, or add " to the end and beginning of each line:
dialog.attachment = [NSString
stringWithFormat:#"{\"name\":\"Facebook Connect for"
"iPhone\",\"href\":\"http://developers.facebook.com/"
"connect.phptab=iphone\",\"caption\":\"Caption\","
"\"description\":\"Description\",\"media\":[{\"type\":"
"\"image\",\"src\":\"http://img40.yfrog.com/img40/"
"5914/iphoneconnectbtn.jpg\",\"href\":"
"\"http://developers.facebook.com/connect.php?"
"tab=iphone/\"}],\"properties\":{\"another link\":"
"{\"text\":\"Facebook home page\",\"href\":"
"\"http://www.facebook.com\"}}}"];
Also, note that in this case you don't need to use stringWithFormat, you can create the string like this:
dialog.attachment = #"{\"name\":\"Facebook Connect for"
"iPhone\",\"href\":\"http://developers.facebook.com/"
"connect.phptab=iphone\",\"caption\":\"Caption\","
"\"description\":\"Description\",\"media\":[{\"type\":"
"\"image\",\"src\":\"http://img40.yfrog.com/img40/"
"5914/iphoneconnectbtn.jpg\",\"href\":"
"\"http://developers.facebook.com/connect.php?"
"tab=iphone/\"}],\"properties\":{\"another link\":"
"{\"text\":\"Facebook home page\",\"href\":"
"\"http://www.facebook.com\"}}}";
The only thing apparently wrong with the code you've posted is all of the line breaks in the middle of your long string. Also, using stringWithFormat is not necessary there. Also your first 'href' is missing a '?' from the GET query. So try this and see what happens:
dialog.attachment = #"{\"name\":\"Facebook Connect for iPhone\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}";
Or better yet, break all of your keys and values out into their own NSStrings and put the long string back together using stringWithFormat. Or even BETTER, create the whole thing in memory and use NSJSONSerialization to create your JSON string. It will be much cleaner and less prone to errors.
I could be wrong but i think facebook excepts a dictionary of values for each of those keys.
There example code is very good and well documented.
Try replacing " with ' inbetween the start and end of the string.

Resources