Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Created NSURL is null

Tags:

iphone

nsurl

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];
like image 889
Jeeva Avatar asked Feb 05 '11 08:02

Jeeva


2 Answers

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.

like image 63
Infinite Possibilities Avatar answered Nov 19 '22 22:11

Infinite Possibilities


from the docs for -[NSURL initWithString:]:

If the string was malformed, returns nil.

This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.

which raises: what's your input?

like image 25
justin Avatar answered Nov 19 '22 21:11

justin