简而言之:
如果您创建一个网页URL(例如http://example.com/path
),请包含 /
,因为它使用以下模板:
{scheme}://{user}:{password}@{host}:{port}{path}?{query}#{fragment}
如果您正在创建非 Web URL(例如mailto:user@example.com
),请不要包含/
,因为它使用以下模板:
{scheme}:{path}?{query}%23{fragment}
为了澄清这个问题,
URLComponents 的实际模板如下所示:
{scheme}://{user}:{password}@{host}:{port}{path}?{query}#{fragment}
注意每两个组件之间都有标点符号(
://
、
:
、
@
、
:
、
?
、
#
),但
{port}
和
{path}
之间没有。因此,
{path}
必须前缀标点符号(
/
),否则将生成无效的URL。
在您的情况下,您必须使用:
:
。
urlComponents.path = "/auth/login"
请注意,即使您省略了URLComponents的不常见字段,您仍然会遇到相同的问题。
{scheme}://{host}{path}?{query}#{fragment}
注意,在这种情况下,{host}
和 {path}
是相邻的,并且没有标点符号将两者分开。
您可能会想知道为什么 Apple 没有在 {port}
之前包含标点符号。我猜测是因为 URLComponents 可以用于生成RFC 3986规范中的任何类型的 URL,而不仅仅是 Web URL。例如,它可以轻松地生成 http://www.example.com/foo
,但同样容易生成形如 mailto:user@example.com
的 URL。
当省略 {host}
时,URLComponents 通过更改 {scheme}
和 {path}
之间的定界符来实现这一点:
{scheme}:{path}?{query}%23{fragment}
因此,当我们想要创建一个mailto链接时,实际上在路径中不应该包含/
:
urlComponents.scheme = "mailto"
urlComponents.path = "user@example.com"
// urlComponents.url!.absoluteString == "mailto:user@example.com"
比较URLComponents和URL的属性可以提供更多见解(并揭示了一些不一致之处)。
let string = "scheme://user:password@host:123/path/path/path/?query#fragment"
let urlComponents = URLComponents(string: string)!
let url = URL(string: string)!
// these are expected
url.absoluteString == urlComponents.url!.absoluteString
url.scheme == urlComponents.scheme
url.user == urlComponents.user
url.password == urlComponents.password
url.host == urlComponents.host
url.port == urlComponents.port
url.query == urlComponents.query
url.fragment == urlComponents.fragment
// this is unexpected. They should have been equal to each other
url.path != urlComponents.path
url.path == "/path/path/path"
urlComponents.path == "/path/path/path/"
url.pathComponents == ["/", "path", "path", "path"]
url.lastPathComponent == "path"
需要注意两点:
url.path
的后缀没有/
。url.pathComponents[0]
是URL中第一个/
。
要获取模板的详细列表,你可以使用以下代码:
let componentsCount = Double(8)
let iterations = Int(pow(2, componentsCount))
for i in 0..<iterations {
var urlComponents = URLComponents()
var key = ""
if i & 0b10000000 > 0 { urlComponents.scheme = "scheme"; key += "s" } else { key += " " }
if i & 0b01000000 > 0 { urlComponents.user = "user"; key += "u" } else { key += " " }
if i & 0b00100000 > 0 { urlComponents.password = "password"; key += "w" } else { key += " " }
if i & 0b00010000 > 0 { urlComponents.host = "host"; key += "h" } else { key += " " }
if i & 0b00001000 > 0 { urlComponents.port = 123; key += "r" } else { key += " " }
if i & 0b00000100 > 0 { urlComponents.path = "/path"; key += "p" } else { key += " " }
if i & 0b00000010 > 0 { urlComponents.query = "query"; key += "q" } else { key += " " }
if i & 0b00000001 > 0 { urlComponents.fragment = "fragment"; key += "f" } else { key += " " }
if let url = urlComponents.url?.absoluteString {
print("[\(key)] \(url)")
} else {
print("[\(key)] ------- nil --------")
}
}
它会生成以下输出:
[ ]
[ f]
[ q ] ?query
[ qf] ?query
[ p ] /path
[ p f] /path
[ pq ] /path?query
[ pqf] /path?query
[ r ] //:123
[ r f] //:123
[ r q ] //:123?query
[ r qf] //:123?query
[ rp ] //:123/path
[ rp f] //:123/path
[ rpq ] //:123/path?query
[ rpqf] //:123/path?query
[ h ] //host
[ h f] //host
[ h q ] //host?query
[ h qf] //host?query
[ h p ] //host/path
[ h p f] //host/path
[ h pq ] //host/path?query
[ h pqf] //host/path?query
[ hr ] //host:123
[ hr f] //host:123
[ hr q ] //host:123?query
[ hr qf] //host:123?query
[ hrp ] //host:123/path
[ hrp f] //host:123/path
[ hrpq ] //host:123/path?query
[ hrpqf] //host:123/path?query
[ w ] //:password@
[ w f] //:password@#fragment
[ w q ] //:password@?query
[ w qf] //:password@?query
[ w p ] //:password@/path
[ w p f] //:password@/path
[ w pq ] //:password@/path?query
[ w pqf] //:password@/path?query
[ w r ] //:password@:123
[ w r f] //:password@:123
[ w r q ] //:password@:123?query
[ w r qf] //:password@:123?query
[ w rp ] //:password@:123/path
[ w rp f] //:password@:123/path
[ w rpq ] //:password@:123/path?query
[ w rpqf] //:password@:123/path?query
[ wh ] //:password@host
[ wh f] //:password@host
[ wh q ] //:password@host?query
[ wh qf] //:password@host?query
[ wh p ] //:password@host/path
[ wh p f] //:password@host/path
[ wh pq ] //:password@host/path?query
[ wh pqf] //:password@host/path?query
[ whr ] //:password@host:123
[ whr f] //:password@host:123
[ whr q ] //:password@host:123?query
[ whr qf] //:password@host:123?query
[ whrp ] //:password@host:123/path
[ whrp f] //:password@host:123/path
[ whrpq ] //:password@host:123/path?query
[ whrpqf] //:password@host:123/path?query
[ u ] //user@
[ u f] //user@#fragment
[ u q ] //user@?query
[ u qf] //user@?query
[ u p ] //user@/path
[ u p f] //user@/path
[ u pq ] //user@/path?query
[ u pqf] //user@/path?query
[ u r ] //user@:123
[ u r f] //user@:123
[ u r q ] //user@:123?query
[ u r qf] //user@:123?query
[ u rp ] //user@:123/path
[ u rp f] //user@:123/path
[ u rpq ] //user@:123/path?query
[ u rpqf] //user@:123/path?query
[ u h ] //user@host
[ u h f] //user@host
[ u h q ] //user@host?query
[ u h qf] //user@host?query
[ u h p ] //user@host/path
[ u h p f] //user@host/path
[ u h pq ] //user@host/path?query
[ u h pqf] //user@host/path?query
[ u hr ] //user@host:123
[ u hr f] //user@host:123
[ u hr q ] //user@host:123?query
[ u hr qf] //user@host:123?query
[ u hrp ] //user@host:123/path
[ u hrp f] //user@host:123/path
[ u hrpq ] //user@host:123/path?query
[ u hrpqf] //user@host:123/path?query
[ uw ] //user:password@
[ uw f] //user:password@#fragment
[ uw q ] //user:password@?query
[ uw qf] //user:password@?query
[ uw p ] //user:password@/path
[ uw p f] //user:password@/path
[ uw pq ] //user:password@/path?query
[ uw pqf] //user:password@/path?query
[ uw r ] //user:password@:123
[ uw r f] //user:password@:123
[ uw r q ] //user:password@:123?query
[ uw r qf] //user:password@:123?query
[ uw rp ] //user:password@:123/path
[ uw rp f] //user:password@:123/path
[ uw rpq ] //user:password@:123/path?query
[ uw rpqf] //user:password@:123/path?query
[ uwh ] //user:password@host
[ uwh f] //user:password@host
[ uwh q ] //user:password@host?query
[ uwh qf] //user:password@host?query
[ uwh p ] //user:password@host/path
[ uwh p f] //user:password@host/path
[ uwh pq ] //user:password@host/path?query
[ uwh pqf] //user:password@host/path?query
[ uwhr ] //user:password@host:123
[ uwhr f] //user:password@host:123
[ uwhr q ] //user:password@host:123?query
[ uwhr qf] //user:password@host:123?query
[ uwhrp ] //user:password@host:123/path
[ uwhrp f] //user:password@host:123/path
[ uwhrpq ] //user:password@host:123/path?query
[ uwhrpqf] //user:password@host:123/path?query
[s ] scheme:
[s f] scheme:%23fragment
[s q ] scheme:?query
[s qf] scheme:?query%23fragment
[s p ] scheme:/path
[s p f] scheme:/path
[s pq ] scheme:/path?query
[s pqf] scheme:/path?query
[s r ] scheme://:123
[s r f] scheme://:123
[s r q ] scheme://:123?query
[s r qf] scheme://:123?query
[s rp ] scheme://:123/path
[s rp f] scheme://:123/path
[s rpq ] scheme://:123/path?query
[s rpqf] scheme://:123/path?query
[s h ] scheme://host
[s h f] scheme://host
[s h q ] scheme://host?query
[s h qf] scheme://host?query
[s h p ] scheme://host/path
[s h p f] scheme://host/path
[s h pq ] scheme://host/path?query
[s h pqf] scheme://host/path?query
[s hr ] scheme://host:123
[s hr f] scheme://host:123
[s hr q ] scheme://host:123?query
[s hr qf] scheme://host:123?query
[s hrp ] scheme://host:123/path
[s hrp f] scheme://host:123/path
[s hrpq ] scheme://host:123/path?query
[s hrpqf] scheme://host:123/path?query
[s w ] scheme://:password@
[s w f] scheme://:password@#fragment
[s w q ] scheme://:password@?query
[s w qf] scheme://:password@?query
[s w p ] scheme://:password@/path
[s w p f] scheme://:password@/path
[s w pq ] scheme://:password@/path?query
[s w pqf] scheme://:password@/path?query
[s w r ] scheme://:password@:123
[s w r f] scheme://:password@:123
[s w r q ] scheme://:password@:123?query
[s w r qf] scheme://:password@:123?query
[s w rp ] scheme://:password@:123/path
[s w rp f] scheme://:password@:123/path
[s w rpq ] scheme://:password@:123/path?query
[s w rpqf] scheme://:password@:123/path?query
[s wh ] scheme://:password@host
[s wh f] scheme://:password@host
[s wh q ] scheme://:password@host?query
[s wh qf] scheme://:password@host?query
[s wh p ] scheme://:password@host/path
[s wh p f] scheme://:password@host/path
[s wh pq ] scheme://:password@host/path?query
[s wh pqf] scheme://:password@host/path?query
[s whr ] scheme://:password@host:123
[s whr f] scheme://:password@host:123
[s whr q ] scheme://:password@host:123?query
[s whr qf] scheme://:password@host:123?query
[s whrp ] scheme://:password@host:123/path
[s whrp f] scheme://:password@host:123/path
[s whrpq ] scheme://:password@host:123/path?query
[s whrpqf] scheme://:password@host:123/path?query
[su ] scheme://user@
[su f] scheme://user@#fragment
[su q ] scheme://user@?query
[su qf] scheme://user@?query
[su p ] scheme://user@/path
[su p f] scheme://user@/path
[su pq ] scheme://user@/path?query
[su pqf] scheme://user@/path?query
[su r ] scheme://user@:123
[su r f] scheme://user@:123
[su r q ] scheme://user@:123?query
[su r qf] scheme://user@:123?query
[su rp ] scheme://user@:123/path
[su rp f] scheme://user@:123/path
[su rpq ] scheme://user@:123/path?query
[su rpqf] scheme://user@:123/path?query
[su h ] scheme://user@host
[su h f] scheme://user@host
[su h q ] scheme://user@host?query
[su h qf] scheme://user@host?query
[su h p ] scheme://user@host/path
[su h p f] scheme://user@host/path
[su h pq ] scheme://user@host/path?query
[su h pqf] scheme://user@host/path?query
[su hr ] scheme://user@host:123
[su hr f] scheme://user@host:123
[su hr q ] scheme://user@host:123?query
[su hr qf] scheme://user@host:123?query
[su hrp ] scheme://user@host:123/path
[su hrp f] scheme://user@host:123/path
[su hrpq ] scheme://user@host:123/path?query
[su hrpqf] scheme://user@host:123/path?query
[suw ] scheme://user:password@
[suw f] scheme://user:password@#fragment
[suw q ] scheme://user:password@?query
[suw qf] scheme://user:password@?query
[suw p ] scheme://user:password@/path
[suw p f] scheme://user:password@/path
[suw pq ] scheme://user:password@/path?query
[suw pqf] scheme://user:password@/path?query
[suw r ] scheme://user:password@:123
[suw r f] scheme://user:password@:123
[suw r q ] scheme://user:password@:123?query
[suw r qf] scheme://user:password@:123?query
[suw rp ] scheme://user:password@:123/path
[suw rp f] scheme://user:password@:123/path
[suw rpq ] scheme://user:password@:123/path?query
[suw rpqf] scheme://user:password@:123/path?query
[suwh ] scheme://user:password@host
[suwh f] scheme://user:password@host
[suwh q ] scheme://user:password@host?query
[suwh qf] scheme://user:password@host?query
[suwh p ] scheme://user:password@host/path
[suwh p f] scheme://user:password@host/path
[suwh pq ] scheme://user:password@host/path?query
[suwh pqf] scheme://user:password@host/path?query
[suwhr ] scheme://user:password@host:123
[suwhr f] scheme://user:password@host:123
[suwhr q ] scheme://user:password@host:123?query
[suwhr qf] scheme://user:password@host:123?query
[suwhrp ] scheme://user:password@host:123/path
[suwhrp f] scheme://user:password@host:123/path
[suwhrpq ] scheme://user:password@host:123/path?query
[suwhrpqf] scheme://user:password@host:123/path?query
注意,这些中没有一个是nil
。但是,如果您将urlComponents.path = "/path"
更改为urlComponents.path = "path"
,则会得到完全不同的模板:
[ ]
[ f]
[ q ] ?query
[ qf] ?query#fragment
[ p ] path
[ p f] path#fragment
[ pq ] path?query
[ pqf] path?query#fragment
[ r ] //:123
[ r f] //:123#fragment
[ r q ] //:123?query
[ r qf] //:123?query#fragment
[ rp ] ------- nil --------
[ rp f] ------- nil --------
[ rpq ] ------- nil --------
[ rpqf] ------- nil --------
[ h ] //host
[ h f] //host#fragment
[ h q ] //host?query
[ h qf] //host?query#fragment
[ h p ] ------- nil --------
[ h p f] ------- nil --------
[ h pq ] ------- nil --------
[ h pqf] ------- nil --------
[ hr ] //host:123
[ hr f] //host:123#fragment
[ hr q ] //host:123?query
[ hr qf] //host:123?query#fragment
[ hrp ] ------- nil --------
[ hrp f] ------- nil --------
[ hrpq ] ------- nil --------
[ hrpqf] ------- nil --------
[ w ] //:password@
[ w f] //:password@#fragment
[ w q ] //:password@?query
[ w qf] //:password@?query#fragment
[ w p ] ------- nil --------
[ w p f] ------- nil --------
[ w pq ] ------- nil --------
[ w pqf] ------- nil --------
[ w r ] //:password@:123
[ w r f] //:password@:123#fragment
[ w r q ] //:password@:123?query
[ w r qf] //:password@:123?query#fragment
[ w rp ] ------- nil --------
[ w rp f] ------- nil --------
[ w rpq ] ------- nil --------
[ w rpqf] ------- nil --------
[ wh ] //:password@host
[ wh f] //:password@host#fragment
[ wh q ] //:password@host?query
[ wh qf] //:password@host?query#fragment
[ wh p ] ------- nil --------
[ wh p f] ------- nil --------
[ wh pq ] ------- nil --------
[ wh pqf] ------- nil --------
[ whr ] //:password@host:123
[ whr f] //:password@host:123#fragment
[ whr q ] //:password@host:123?query
[ whr qf] //:password@host:123?query#fragment
[ whrp ] ------- nil --------
[ whrp f] ------- nil --------
[ whrpq ] ------- nil --------
[ whrpqf] ------- nil --------
[ u ] //user@
[ u f] //user@#fragment
[ u q ] //user@?query
[ u qf] //user@?query#fragment
[ u p ] ------- nil --------
[ u p f] ------- nil --------
[ u pq ] ------- nil --------
[ u pqf] ------- nil --------
[ u r ] //user@:123
[ u r f] //user@:123#fragment
[ u r q ] //user@:123?query
[ u r qf] //user@:123?query#fragment
[ u rp ] ------- nil --------
[ u rp f] ------- nil --------
[ u rpq ] ------- nil --------
[ u rpqf] ------- nil --------
[ u h ] //user@host
[ u h f] //user@host#fragment
[ u h q ] //user@host?query
[ u h qf] //user@host?query#fragment
[ u h p ] ------- nil --------
[ u h p f] ------- nil --------
[ u h pq ] ------- nil --------
[ u h pqf] ------- nil --------
[ u hr ] //user@host:123
[ u hr f] //user@host:123#fragment
[ u hr q ] //user@host:123?query
[ u hr qf] //user@host:123?query#fragment
[ u hrp ] ------- nil --------
[ u hrp f] ------- nil --------
[ u hrpq ] ------- nil --------
[ u hrpqf] ------- nil --------
[ uw ] //user:password@
[ uw f] //user:password@#fragment
[ uw q ] //user:password@?query
[ uw qf] //user:password@?query#fragment
[ uw p ] ------- nil --------
[ uw p f] ------- nil --------
[ uw pq ] ------- nil --------
[ uw pqf] ------- nil --------
[ uw r ] //user:password@:123
[ uw r f] //user:password@:123#fragment
[ uw r q ] //user:password@:123?query
[ uw r qf] //user:password@:123?query#fragment
[ uw rp ] ------- nil --------
[ uw rp f] ------- nil --------
[ uw rpq ] ------- nil --------
[ uw rpqf] ------- nil --------
[ uwh ] //user:password@host
[ uwh f] //user:password@host#fragment
[ uwh q ] //user:password@host?query
[ uwh qf] //user:password@host?query#fragment
[ uwh p ] ------- nil --------
[ uwh p f] ------- nil --------
[ uwh pq ] ------- nil --------
[ uwh pqf] ------- nil --------
[ uwhr ] //user:password@host:123
[ uwhr f] //user:password@host:123#fragment
[ uwhr q ] //user:password@host:123?query
[ uwhr qf] //user:password@host:123?query#fragment
[ uwhrp ] ------- nil --------
[ uwhrp f] ------- nil --------
[ uwhrpq ] ------- nil --------
[ uwhrpqf] ------- nil --------
[s ] scheme:
[s f] scheme:%23fragment
[s q ] scheme:?query
[s qf] scheme:?query%23fragment
[s p ] scheme:path
[s p f] scheme:path%23fragment
[s pq ] scheme:path?query
[s pqf] scheme:path?query%23fragment
[s r ] scheme://:123
[s r f] scheme://:123#fragment
[s r q ] scheme://:123?query
[s r qf] scheme://:123?query#fragment
[s rp ] ------- nil --------
[s rp f] ------- nil --------
[s rpq ] ------- nil --------
[s rpqf] ------- nil --------
[s h ] scheme://host
[s h f] scheme://host#fragment
[s h q ] scheme://host?query
[s h qf] scheme://host?query#fragment
[s h p ] ------- nil --------
[s h p f] ------- nil --------
[s h pq ] ------- nil --------
[s h pqf] ------- nil --------
[s hr ] scheme://host:123
[s hr f] scheme://host:123#fragment
[s hr q ] scheme://host:123?query
[s hr qf] scheme://host:123?query#fragment
[s hrp ] ------- nil --------
[s hrp f] ------- nil --------
[s hrpq ] ------- nil --------
[s hrpqf] ------- nil --------
[s w ] scheme://:password@
[s w f] scheme://:password@#fragment
[s w q ] scheme://:password@?query
[s w qf] scheme://:password@?query#fragment
[s w p ] ------- nil --------
[s w p f] ------- nil --------
[s w pq ] ------- nil --------
[s w pqf] ------- nil --------
[s w r ] scheme://:password@:123
[s w r f] scheme://:password@:123#fragment
[s w r q ] scheme://:password@:123?query
[s w r qf] scheme://:password@:123?query#fragment
[s w rp ] ------- nil --------
[s w rp f] ------- nil --------
[s w rpq ] ------- nil --------
[s w rpqf] ------- nil --------
[s wh ] scheme://:password@host
[s wh f] scheme://:password@host#fragment
[s wh q ] scheme://:password@host?query
[s wh qf] scheme://:password@host?query#fragment
[s wh p ] ------- nil --------
[s wh p f] ------- nil --------
[s wh pq ] ------- nil --------
[s wh pqf] ------- nil --------
[s whr ] scheme://:password@host:123
[s whr f] scheme://:password@host:123#fragment
[s whr q ] scheme://:password@host:123?query
[s whr qf] scheme://:password@host:123?query#fragment
[s whrp ] ------- nil --------
[s whrp f] ------- nil --------
[s whrpq ] ------- nil --------
[s whrpqf] ------- nil --------
[su ] scheme://user@
[su f] scheme://user@#fragment
[su q ] scheme://user@?query
[su qf] scheme://user@?query#fragment
[su p ] ------- nil --------
[su p f] ------- nil --------
[su pq ] ------- nil --------
[su pqf] ------- nil --------
[su r ] scheme://user@:123
[su r f] scheme://user@:123#fragment
[su r q ] scheme://user@:123?query
[su r qf] scheme://user@:123?query#fragment
[su rp ] ------- nil --------
[su rp f] ------- nil --------
[su rpq ] ------- nil --------
[su rpqf] ------- nil --------
[su h ] scheme://user@host
[su h f] scheme://user@host#fragment
[su h q ] scheme://user@host?query
[su h qf] scheme://user@host?query#fragment
[su h p ] ------- nil --------
[su h p f] ------- nil --------
[su h pq ] ------- nil --------
[su h pqf] ------- nil --------
[su hr ] scheme://user@host:123
[su hr f] scheme://user@host:123#fragment
[su hr q ] scheme://user@host:123?query
[su hr qf] scheme://user@host:123?query#fragment
[su hrp ] ------- nil --------
[su hrp f] ------- nil --------
[su hrpq ] ------- nil --------
[su hrpqf] ------- nil --------
[suw ] scheme://user:password@
[suw f] scheme://user:password@#fragment
[suw q ] scheme://user:password@?query
[suw qf] scheme://user:password@?query#fragment
[suw p ] ------- nil --------
[suw p f] ------- nil --------
[suw pq ] ------- nil --------
[suw pqf] ------- nil --------
[suw r ] scheme://user:password@:123
[suw r f] scheme://user:password@:123#fragment
[suw r q ] scheme://user:password@:123?query
[suw r qf] scheme://user:password@:123?query#fragment
[suw rp ] ------- nil --------
[suw rp f] ------- nil --------
[suw rpq ] ------- nil --------
[suw rpqf] ------- nil --------
[suwh ] scheme://user:password@host
[suwh f] scheme://user:password@host#fragment
[suwh q ] scheme://user:password@host?query
[suwh qf] scheme://user:password@host?query#fragment
[suwh p ] ------- nil --------
[suwh p f] ------- nil --------
[suwh pq ] ------- nil --------
[suwh pqf] ------- nil --------
[suwhr ] scheme://user:password@host:123
[suwhr f] scheme://user:password@host:123#fragment
[suwhr q ] scheme://user:password@host:123?query
[suwhr qf] scheme://user:password@host:123?query#fragment
[suwhrp ] ------- nil --------
[suwhrp f] ------- nil --------
[suwhrpq ] ------- nil --------
[suwhrpqf] ------- nil --------