Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between URL.standardized and URL.absoluteURL?

Tags:

url

swift

In Swift's URL class, there are .standardized and .absoluteURL properties (.standardizedURL and .absoluteURL, and .URLByStandardizingPath in Objective-C's NSURL). There's also .standardizedFileURL.

I have no idea what the difference is between all these, but there must be one since they provide all of them, right? I mean clearly the .standardizedFileURL is meant to only deal with file URLs, but other than that (for instance, if all are called on a file URL), what's the difference?


From the above-linked docs:

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.

Objective-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).

like image 605
Ky. Avatar asked Apr 06 '17 14:04

Ky.


People also ask

What is difference between absolute URL and relative URL?

An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.

What is absolute URL example?

An absolute URL contains all the information necessary to locate a resource. In context to the Cart.com online stores system it begins with https://. For example, https://cart.com is an absolute URL.

What is the absolute URL for a webpage called?

What is an absolute URL? An absolute URL is the full URL, including protocol ( http / https ), the optional subdomain (e.g. www ), domain ( example.com ), and path (which includes the directory and slug). Absolute URLs provide all the available information to find the location of a page.


1 Answers

If url is an absolute URL, url.absoluteURL == url.

If url is a relative URL and has a non-nil baseURL, then url.absoluteURL returns an absolute URL by resolving the relativity of url in the context of baseURL (and thus url.absoluteURL != url).

If url is a relative URL, url.standardized does not return an absolute URL, and may in fact return a URL that does not resolve the same way as url (!) because url.standardized removes any leading .. components of the path.

Example:

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"
like image 78
rob mayoff Avatar answered Oct 07 '22 23:10

rob mayoff