📦URL
Last updated
Last updated
frameworks ⟩ Foundation ⟩ URL
var parts = URLComponents()
parts.scheme = "https"
parts.host = "example.com"
parts.path = "/v1/player"
parts.queryItems = [
URLQueryItem(name: "name", value: "艾佛森")
]
// ⭐️ URLQueryItem 會自動做 URL encoding,所以有空格或中文都沒關係。
// ╭───────── query item ─────────╮
// https://example.com/v1/player?name=%E8%89%BE%E4%BD%9B%E6%A3%AE
if let url = parts.url { ... }
只有以下所列的 ASCII 字元能出現在網址裡,其它文字生成 URL 時將得到 nil。
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789-._~:/?#[]@!$&'()*+,;=`.
appendingPathComponent(_:) may read file metadata to determine whether the resulting path is a directory. This is done synchronously, and may have significant performance costs if the receiver is a location on a network mounted filesystem.
call appendingPathComponent(_:isDirectory:)
instead if you know whether the resulting path is a directory to avoid this file metadata operation.
https://bob:bobby@www.lunatech.com:8080/file;p=1?q=2#third
\___/ \_/ \___/ \______________/ \__/\_______/ \_/ \___/
| | | | | | \_/ | |
Scheme User Password Host Port Path | | Fragment
\_____________________________/ | Query
| Path parameter
Authority
+-------------------+---------------------+
| Part | Data |
+-------------------+---------------------+
| Scheme | https |
| User | bob |
| Password | bobby |
| Host | www.lunatech.com |
| Port | 8080 |
| Path | /file;p=1 |
| Path parameter | p=1 |
| Query | q=2 |
| Fragment | third |
+-------------------+---------------------+
Foundation ⟩ Numbers, Data, and Basic Values ⟩
appendingPathComponent(_:) 👉 compare: URL ⟩ appendingPathComponent(_:)
addingPercentEncoding(withAllowedCharacters:) - 將非 ASCII 字元轉換成 % 搭配十六進位數字 (URL encoded)
FileManager uses URLs to locate files.