URL host comparison

£可爱£侵袭症+ 提交于 2020-01-16 11:20:07

问题


Here I am trying to compare the hosts of 2 URLs. Even though that hosts are the same it does not respond why!

Code:

NSURL *url=[NSURL URLWithString:@"http://www.facebook.com/"?ref=logo" ];
    NSURL *domain=[ NSURL URLWithString:@"http://www.facebook.com" ];

    if ( [url host]==[domain host] ) {

        NSLog(@"hosts are matched");
    }else {
        NSLog(@"hosts are not matched!");
    }

回答1:


You need to use isEqualToString:

NSURL *url=[NSURL URLWithString:@"http://www.facebook.com/?ref=logo" ];
NSURL *domain=[ NSURL URLWithString:@"http://www.facebook.com" ];

if ( [[url host] isEqualToString: [domain host]] ) {
    NSLog(@"hosts are matched");
}else {
    NSLog(@"hosts are not matched!");
}

Result

2011-11-23 11:55:06.182 TestApp[14404:207] hosts are matched



来源:https://stackoverflow.com/questions/8239274/url-host-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!