问题
I am trying to parse data from a xml web service but I am not able to get my url. Please check my code:
NSLog(@"log url: %@",url);
[url stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSLog(@"%@",url);
[url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSLog(@"%@",url);
NSURL *URL = [NSURL URLWithString:url];
NSXMLParser *home_Parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[home_Parser setDelegate:self];
[home_Parser parse];
[home_Parser release];
I am always getting URL as nil. The reason is my string url has space included. I have tried to replace it and escape it but it is not working. Following is the url that I am using
http://www.mydomain.com/radioListGenre.php?genre=Radiostations playing Rock how can I remove this space. Is there any other technique the those I used above.
Thanks
Pankaj
回答1:
stringByAddingPercentEscapesUsingEncoding
and stringByReplacingOccurrencesOfString
operate on the string provided and return a string. You're not assigning the return value to anything so url isn't changing, the return value is just vanishing into space. You want something more like:
url = [url stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
assuming url is a mutable string. Now url contains the modified version of url. Without the assignment it's effective the same as doing something like this:
a + 1;
instead of:
a = a + 1
来源:https://stackoverflow.com/questions/4637799/not-able-to-create-valid-nsurl