Class
NSURL
An object representing the location of a resource that bridges to URL
; use NSURL
when you need reference semantics or other Foundation-specific behavior.
--表征桥接到URL的资源位置的对象;当您需要引用语义或其他特定于Foundation的行为时,请使用NSURL。
--是一个对象,有自己的行为的,而不是单纯的字符串路径名
Declaration
class NSURL : NSObject
Overview
You can use URL objects to construct URLs and access their parts. For URLs that represent local files, you can also manipulate properties of those files directly, such as changing the file’s last modification date. Finally, you can pass URL objects to other APIs to retrieve the contents of those URLs. For example, you can use the URLSession
, NSURLConnection
, and NSURLDownload
classes to access the contents of remote resources, as described in URL Loading System.
--可以使用URL对象来构造URL并访问其部分。 对于表示本地文件的URL,您还可以直接操作这些文件的属性,例如更改文件的上次修改日期。 最后,您可以将URL对象传递给其他API以检索这些URL的内容。 例如,您可以使用URLSession、NSURLConnection和NSURLDownload类访问远程资源的内容,如URL Loading System中所述。
URL objects are the preferred way to refer to local files. Most objects that read data from or write data to a file have methods that accept an NSURL
object instead of a pathname as the file reference. For example, you can get the contents of a local file URL as an NSString
object using the init(contentsOf:encoding:)
initializer, or as an NSData
object using the init(contentsOf:options:)
initializer.
--URL对象是引用本地文件的首选方式。 大多数从文件读取数据或向文件写入数据的对象都有接受NSURL对象(而不是路径名)作为文件引用的方法。 例如,您可以使用NSString的init(contentsOf:coding:)构造器传入URL作为参数,将本地文件URL的内容作为NSString对象,或者使用NSData的init(contentsOf:options:)构造器参数传入,将URL的内容作为NSData对象。
You can also use URLs for interapplication communication. In macOS, the NSWorkspace
class provides the open(_:)
method to open a location specified by a URL. Similarly, in iOS, the UIApplication
class provides the open(_:options:completionHandler:)
method.
Additionally, you can use URLs when working with pasteboards, as described in NSURL Additions Reference (part of the AppKit framework).
The NSURL
class is “toll-free bridged” with its Core Foundation counterpart, CFURL
. See Toll-Free Bridging for more information on toll-free bridging.
Important
The Swift overlay to the Foundation framework provides the URL
structure, which bridges to the NSURL
class. For more information about value types, see Classes and Structures in The Swift Programming Language (Swift 4.1) and Working with Cocoa Frameworks in Using Swift with Cocoa and Objective-C (Swift 4.1).
Structure of a URL
An NSURL
object is composed of two parts—a potentially nil
base URL and a string that is resolved relative to the base URL. An NSURL
object is considered absolute if its string part is fully resolved without a base; all other URLs are considered relative.
For example, when constructing an NSURL
object, you might specify file:///path/to/user/
as the base URL and folder/file.html
as the string part, as follows:
NSURL *baseURL = [NSURL fileURLWithPath:@"file:///path/to/user/"];
NSURL *URL = [NSURL URLWithString:@"folder/file.html" relativeToURL:baseURL];
NSLog(@"absoluteURL = %@", [URL absoluteURL]);
When fully resolved, the absolute URL is file:///path/to/user/folder/file.html
.
A URL can be also be divided into pieces based on its structure. For example, the URL https://johnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref
contains the following URL components:
Component |
Value |
---|---|
|
|
|
|
|
|
|
|
|
|