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:
standardized
Returns a URL with any instances of “..” or “.” removed from its path.
absoluteURL
Returns the absolute URL.
Discussion
If the URL is itself absolute, this will return self.
standardizedFileURL
Standardizes the path of a file URL.
Discussion
If the
isFileURL
isfalse
, this method returnsself
.
standardizedURL
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.
absoluteURL
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.
URLByStandardizingPath
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
stringByStandardizingPath
, this property can make the following changes in the provided URL:Expand an initial tilde expression using
stringByExpandingTildeInPath
. 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 usingstringByResolvingSymlinksInPath
, 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).
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.
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 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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With