gpt4 book ai didi

swift - URL.standardized 和 URL.absoluteURL 有什么区别?

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:16 28 4
gpt4 key购买 nike

在Swift的URL类中,有.standardized.absoluteURL属性(.standardizedURL.absoluteURL 以及 Objective-C 的 NSURL 中的 .URLByStandardizingPath)。还有 .standardizedFileURL .

我不知道所有这些之间有什么区别,但必须有一个,因为它们提供了所有这些,对吗?我的意思很明确,.standardizedFileURL 仅用于处理文件 URL,但除此之外(例如,如果所有文件 URL 都被调用),有什么区别?


来自上面链接的文档:

swift

standardized

Returns a URL with any instances of “..” or “.” removed from its path.

absolute​URL

Returns the absolute URL.

Discussion

If the URL is itself absolute, this will return self.

standardized​File​URL

Standardizes the path of a file URL.

Discussion

If the is​File​URL is false, this method returns self.

目标-C

standardized​URL

A copy of the URL with any instances of ".." or "." removed from its path. (read-only)

Discussion

This property contains a new NSURL object, initialized using the receiver’s path with any instances of ".." or "." removed. If the receiver does not conform to RFC 1808, this property contains nil.

absolute​URL

An absolute URL that refers to the same resource as the receiver. (read-only)

Discussion

If the URL is already absolute, this property contains a copy of the receiver. Resolution is performed per RFC 1808.

URLBy​Standardizing​Path

A URL that points to the same resource as the original URL using an absolute path. (read-only)

Discussion

This property only works on URLs with the file:​ path scheme. For all other URLs, it returns a copy of the original URL.

Like string​By​Standardizing​Path, this property can make the following changes in the provided URL:

Expand an initial tilde expression using string​By​Expanding​Tilde​In​Path. Reduce empty components and references to the current directory (that is, the sequences “//” and “/./”) to single path separators. In absolute paths only, resolve references to the parent directory (that is, the component “..”) to the real parent directory if possible using string​By​Resolving​Symlinks​In​Path, which consults the file system to resolve each potential symbolic link.

In relative paths, because symbolic links can’t be resolved, references to the parent directory are left in place. Remove an initial component of “/private” from the path if the result still indicates an existing file or directory (checked by consulting the file system).

Note that the path contained by this property may still have symbolic link components in it. Note also that this property only works with file paths (not, for example, string representations of URLs).

最佳答案

如果 url 是绝对 URL,url.absoluteURL == url

如果url是一个相对URL并且有一个非nil的baseURL,那么url.absoluteURL通过解析相对性返回一个绝对URL urlbaseURL 的上下文中(因此 url.absoluteURL != url)。

如果 url 是一个相对 URL,url.standardized 不会返回一个绝对 URL,实际上可能会返回一个 URL与 url (!) 的解析方式不同,因为 url.standardized 删除了路径的所有前导 .. 组件。

例子:

let base = URL(string: "https://stackoverflow.com/q/43258046/77567")!
// output: "https://stackoverflow.com/q/43258046/77567"

let rel = URL(string: "../16176911", relativeTo: base)!
// output: "../../16176911 -- ttps://stackoverflow.com/q/43258046/77567"

rel.absoluteURL
// output: "https://stackoverflow.com/q/16176911"

rel.standardized
// output: "16176911 -- ttps://stackoverflow.com/q/43258046/77567"

rel.standardized.absoluteURL
// output: "https://stackoverflow.com/q/43258046/16176911"

关于swift - URL.standardized 和 URL.absoluteURL 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43258046/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com