比较两个NSURL或一个NSURL和一个NSString的可靠方法? [英] Reliable way to compare two NSURL or one NSURL and an NSString?

查看:320
本文介绍了比较两个NSURL或一个NSURL和一个NSString的可靠方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在比较两个NSURL并将一个NSURL与一个NSString(这是一个URL地址)比较时遇到问题,情况是我从某个地方得到了NSURLRequest,我可能知道或可能不知道它指向的URL地址,并且我有一个URL NSString,例如"http://m.google.com",现在我需要检查该NSURLRequest中的URL是否与我拥有的URL字符串相同:

I recently had a problem when comparing two NSURLs and compare one NSURL with an NSString(which is a URL address), the situation is I got an NSURLRequest from somewhere, I may or may not know the URL address it points to, and I have an URL NSString, say "http://m.google.com", now I need to check if the URL in that NSURLRequest is the same as the URL string I had:

[[request.URL.absoluteString lowercaseString] isEqualToString: [self.myAddress lowercaseString]];

这将返回否,因为absoluteString会给我"http://m.google.com/",而我的字符串是"http://m.google.com",即使最后我也没有反斜杠使用

this returns NO as the absoluteString gives me "http://m.google.com/" whereas my string is "http://m.google.com" without a slash in the end, even if I create the NSURLRequest using

[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.google.com"]]

它仍然为我提供absoluteString的"http://m.google.com/",我想知道是否有任何可靠的方法可以与NSURL或一个NSURL和一个NSString进行比较?

it still gives me "http://m.google.com/" for absoluteString, I wonder is there any reliable way to compare to NSURL or one NSURL and one NSString?

  1. 检查一个是否包含"另一个,但这不可靠,因为"http://m.google.com/blabla"包含"http://m.google.com".

  1. check if one 'contains' another, but this is not reliable as 'http://m.google.com/blabla' contains 'http://m.google.com'.

将NSString转换为NSURL并使用isEqual方法比较两个NSURL,希望NSURL的isEqual实现能够弄清楚吗?

convert the NSString to NSURL and use the isEqual method to compare two NSURL and hopefully NSURL's implementation of isEqual can figure it out?

基于第2步,但使用standardizedURL?

非常感谢!

推荐答案

如果仅关心尾部斜杠的歧义性,则可以通过知道NSURL路径来修剪尾部斜杠来快速省却这个问题.

If you care only about the trailing slash ambiguity, you can dispense with this question quickly by knowing that NSURL path trims the trailing slash.

但是我喜欢NSURL上的类别方法的想法,该方法实现了一些基于标准的对等(在这种情况下,对等"可能比对等更好).

But I like the idea of a category method on NSURL that implements some standards-based equivalence ("equivalence" is probably a better term than equality in this case).

@RobNapier指的是一个相关问题,答案很好,指向 RFC2616 .网址语法的另一个相关标准是 RFC1808 .

@RobNapier refers to a related question with a good answer that points to RFC2616. Another relevant standard for url syntax is RFC1808.

困难的部分是确定等效性是什么意思,例如,不同的查询或片段(锚链接)如何处理?下面的代码对于大多数此类歧义都是错误的……

The tough part is deciding what we mean by equivalence, for example, what about differing queries or fragments (anchor links)? The code below errs on the side of permissiveness for most of these ambiguities...

// in NSURL+uriEquivalence.m

- (BOOL)isEquivalent:(NSURL *)aURL {

    if ([self isEqual:aURL]) return YES;
    if ([[self scheme] caseInsensitiveCompare:[aURL scheme]] != NSOrderedSame) return NO;
    if ([[self host] caseInsensitiveCompare:[aURL host]] != NSOrderedSame) return NO;

    // NSURL path is smart about trimming trailing slashes
    // note case-sensitivty here
    if ([[self path] compare:[aURL path]] != NSOrderedSame) return NO;

    // at this point, we've established that the urls are equivalent according to the rfc
    // insofar as scheme, host, and paths match

    // according to rfc2616, port's can weakly match if one is missing and the
    // other is default for the scheme, but for now, let's insist on an explicit match
    if ([self port] || [aURL port]) {
        if (![[self port] isEqual:[aURL port]]) return NO;
        if (![[self query] isEqual:[aURL query]]) return NO;
    }

    // for things like user/pw, fragment, etc., seems sensible to be
    // permissive about these.
    return YES;
}

这篇关于比较两个NSURL或一个NSURL和一个NSString的可靠方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆