Not able to create valid nsurl

梦想与她 提交于 2019-12-12 06:13:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!