iphone - 创建的 NSURL 为空

标签 iphone nsurl

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/

相关文章:

ios - Objective-C:从可空指针 'NSURL * _Nullable' 到不可空指针类型 'NSURL * _Nonnull'

ios - 单点触控 : Problems downloading and playing video from external URL

ios - 设置两个动态标签之间的距离

iPhone蓝图/素描/绘图软件

ios - 如何为 React Native 构建 .ipa?

iphone - UIWebView 从不显示滚动条

ios - 如何快速获取从图库中选择的图像路径

ios - 支付你的钱 nsurl 错误空字符串

swift - 从 UIImagePickerController 获取 NSURL

iphone - 手机上的语义东西(RDF、OWL)——这可能吗?