如何比较实际上相当但具有化妆品字符串差异的两个NSURL

如何比较实际上相当但具有化妆品字符串差异的两个NSURL

本文介绍了如何比较实际上相当但具有化妆品字符串差异的两个NSURL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IOS下,我希望识别成对的 NSURL ,它们相同或稍有不同但引用相同的目的地。例如 http://google.com vs http://google.com/

Under IOS, I wish to identify pairs of NSURLs that are either identical or differ slightly but refer to the same destination. For instance http://google.com vs http://google.com/.

isEquals 报告这些NSURL是不同的。我也无法找到获得NSURL的规范或规范化形式的方法来比较这些形式。

isEquals reports those NSURLs as diferent. Nor can I find a way to obtain a 'canonical' or 'normalized' form of an NSURL to compare those forms instead.

NSURL *a = [NSURL URLWithString:@"http://google.com"];
NSURL *b = [NSURL URLWithString:@"http://google.com/"];

if([a isEqual:b]) {
  NSLog(@"Same"); // Not run
}

if([[a absoluteURL] isEqual:[b absoluteURL]]) {
  NSLog(@"Same"); // Still not run
}

if([[a URLByStandardizingPath] isEqual:[b URLByStandardizingPath]]) {
  NSLog(@"Same"); // Still not run
}

如何比较两个NSURL而忽略字符串差异不影响URL的意图吗?

How can I compare two NSURLs while ignoring string differences that do not affect the intent of the URL?

推荐答案

根据, http://google.com http:/ /google.com / 是等效的。这就是它所说的(第3.2.3节):

According to RFC 2616, http://google.com and http://google.com/ are equivalent. This is what it says (section 3.2.3):


  • 一个空的端口或未给出等同于该URI引用的默认端口;

  • A port that is empty or not given is equivalent to the default port for that URI-reference;

主机名的比较必须不区分大小写;

Comparisons of host names MUST be case-insensitive;

方案名称的比较必须不区分大小写;

Comparisons of scheme names MUST be case-insensitive;

空的abs_path相当于abs_path的/ 。

An empty abs_path is equivalent to an abs_path of "/".

供参考,语法为 http_URL =http://host [:port] [abs_path [?查询]]

不幸的是, NSURL 必须支持HTTP以外的方案,所以它不能假设RFC 2616提供了通用规则。我认为您最好的解决方法是使用您自己的http网址比较方法创建一个类别,专门检查空的绝对路径。

Unfortunately, NSURL has to support schemes other than HTTP, so it cannot make the assumption that RFC 2616 provides a generic rule. I think your best fix is to create a category with your own compare method for http URLs that specifically checks for an empty absolute path.

这篇关于如何比较实际上相当但具有化妆品字符串差异的两个NSURL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:23