📦URL
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.

Foundation ⟩ Numbers, Data, and Basic Values ⟩
appendingPathComponent(_:) 👉 compare: URL ⟩ appendingPathComponent(_:)
addingPercentEncoding(withAllowedCharacters:) - 將非 ASCII 字元轉換成 % 搭配十六進位數字 (URL encoded)
FileManager uses URLs to locate files.
Last updated
Was this helpful?