Search code examples
iosobjective-cxcodensstringnsurl

Xcode: NSString to NSUrl conversion issue


I am having a strange issue while converting a NSString to NSUrl. Actually I have a method which takes NSString as a parameter and then converts that to NSUrl and then I use that URL to download a file.

When I call this method through my code by passing a url as string, the following line returns nil.

NSURL *url = [NSURL URLWithString:urlString];

But when I hard-code the string within same method, and then I convert the string to NSUrl, I get a proper URL. I don't know why this is happening. Can someone comment on this behaviour and suggest me some way to cope this issue?

//NSString *urlString = @"http://someimage.png"; // works that way but not when I send this urlstring as parameter of method
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"Url after conversion %@", url);

Solution

  • Thanks guys for your answers. I finally have fixed the issue and the issue was in the URL string I was passing as a parameter. It had some extra "" at the ends which were not showing up in the NSLog as it was a normal string, but when I removed those "" from the url string and then converted it to NSURL, it worked fine.