Created NSURL is null

Jeeva picture Jeeva · Feb 5, 2011 · Viewed 10.5k times · Source

NSURL printing null. What's the reason?

NSString *webStr = [[NSString alloc] initWithFormat:@"%@",[webArray objectAtIndex:1]];

NSLog(@"urlString = %@",webStr); // its printing correct url string

NSURL *webURL = [[NSURL alloc] initWithString:webStr];

NSLog(@"url = %@",webURL); // its printing null

[webURL release];

[webStr release];

Answer

Infinite Possibilities picture Infinite Possibilities · Feb 5, 2011

You should do the following.

NSString *webStr = [[NSString alloc] initWithFormat:@"%@",[webArray objectAtIndex:1]];

NSLog(@"urlString = %@",webStr); // its printing correct url string

NSURL *webURL = [[NSURL alloc] initWithString:[webStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

NSLog(@"url = %@",webURL); // it should print it

[webURL release];

[webStr release];

I have used NSASCIIStringEncoding but you can use UTF8 too or any other encoding.