iOS NSURL總結
阿新 • • 發佈:2019-02-14
NSURL用於網路請求或本地檔案相關操作。
網路請求
初始化方法也不同:2個例項方法,2個類方法。
此時url一些關於url請求,一些屬性:- (nullable instancetype)initWithString:(NSString *)URLString; - (nullable instancetype)initWithString:(NSString *)URLString relativeToURL:(nullable NSURL *)baseURL NS_DESIGNATED_INITIALIZER; + (nullable instancetype)URLWithString:(NSString *)URLString; + (nullable instancetype)URLWithString:(NSString *)URLString relativeToURL:(nullable NSURL *)baseURL;
@property (nullable, readonly, copy) NSString *host; @property (nullable, readonly, copy) NSNumber *port; @property (nullable, readonly, copy) NSString *user; @property (nullable, readonly, copy) NSString *password; @property (nullable, readonly, copy) NSString *path; @property (nullable, readonly, copy) NSString *fragment; @property (nullable, readonly, copy) NSString *parameterString; @property (nullable, readonly, copy) NSString *query; @property (nullable, readonly, copy) NSString *relativePath; // The same as path if baseURL is nil
檔案初始化方法:
- (instancetype)initFileURLWithPath:(NSString *)path isDirectory:(BOOL)isDir relativeToURL:(nullable NSURL *)baseURL NS_AVAILABLE(10_11, 9_0) NS_DESIGNATED_INITIALIZER; /* Initializes a newly created file NSURL referencing the local file or directory at path, relative to a base URL. */ - (instancetype)initFileURLWithPath:(NSString *)path relativeToURL:(nullable NSURL *)baseURL NS_AVAILABLE(10_11, 9_0) NS_DESIGNATED_INITIALIZER; // Better to use initFileURLWithPath:isDirectory:relativeToURL: if you know if the path is a directory vs non-directory, as it saves an I/O. - (instancetype)initFileURLWithPath:(NSString *)path isDirectory:(BOOL)isDir NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER; - (instancetype)initFileURLWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER; // Better to use initFileURLWithPath:isDirectory: if you know if the path is a directory vs non-directory, as it saves an i/o. /* Initializes and returns a newly created file NSURL referencing the local file or directory at path, relative to a base URL. */ + (NSURL *)fileURLWithPath:(NSString *)path isDirectory:(BOOL) isDir relativeToURL:(nullable NSURL *)baseURL NS_AVAILABLE(10_11, 9_0); /* Initializes and returns a newly created file NSURL referencing the local file or directory at path, relative to a base URL. */ + (NSURL *)fileURLWithPath:(NSString *)path relativeToURL:(nullable NSURL *)baseURL NS_AVAILABLE(10_11, 9_0); // Better to use fileURLWithPath:isDirectory:relativeToURL: if you know if the path is a directory vs non-directory, as it saves an I/O. + (NSURL *)fileURLWithPath:(NSString *)path isDirectory:(BOOL)isDir NS_AVAILABLE(10_5, 2_0); + (NSURL *)fileURLWithPath:(NSString *)path; // Better to use fileURLWithPath:isDirectory: if you know if the path is a directory vs non-directory, as it saves an i/o. /* Initializes a newly created URL referencing the local file or directory at the file system representation of the path. File system representation is a null-terminated C string with canonical UTF-8 encoding. */ - (instancetype)initFileURLWithFileSystemRepresentation:(const char *)path isDirectory:(BOOL)isDir relativeToURL:(nullable NSURL *)baseURL NS_AVAILABLE(10_9, 7_0) NS_DESIGNATED_INITIALIZER;