URL主机名返回null [英] URL host name returns null
问题描述
我需要获取字符串url的主机名
I need to get the hostname of a string url
我试图从字符串创建NSURL对象,然后使用NSURL的host属性获取主机名.但是主机名始终为空.
I am trying to create the NSURL object from the string and then using host property of NSURL to get the host name. But Host name is always null.
NSURL *url = [NSURL URLWithString: @"www.google.com"];
NSLog(@"Host name %@", url.host);
,但是主机名始终为空.谁能解释为什么主机名为空?
but the host name is always null. can anybody explain why is host name null?
很抱歉,我在问问题时错过了一个引号和逗号,这是一个错字.但是问题仍然存在.
I am sorry there was a typo i missed a quote and a comma while asking the question. But the problem is still the same.
推荐答案
按照 返回值:
URL的主机.如果接收者不符合
RFC 1808
,返回nil
因此,如果它不符合 RFC 1808
,它将返回nil.根据 RFC 1808
So if it is not conforming to RFC 1808
, it will return nil. As per definition of RFC 1808
,
正确的语法如下:
<scheme>://<net_loc>/<path>;<params>?<query>#<fragment>
因此您还需要在形成 NSURL
时添加 http://
.
So you need to add http://
also while forming NSURL
.
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSLog(@"Host name %@", url.host);
这篇关于URL主机名返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!