NSURL打印null。什么原因?

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];

最佳答案

您应该执行以下操作。

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];

我使用了NSASCIIStringEncoding,但是您也可以使用UTF8或任何其他编码。

关于iphone - 创建的NSURL为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4905843/

10-10 06:38