我正在尝试使用 URLComponents
来组成一个 URL
,因为这似乎就是它的用途。
但是,当我随后访问组件的 url
属性时,它是 nil。
例子...
var urlComponents = URLComponents(string: "http://google.com")!
urlComponents.path = "auth/login"
然后我...
print(urlComponents)
输出...
scheme: http host: google.com path: auth/login
- scheme : "http"
- host : "google.com"
- path : "auth/login"
但是……
print(urlComponents.url)
输出nil
。
我这样做有问题吗?我如何从所有这些中获得完整的 URL?谢谢
最佳答案
看起来path
参数的字符串必须以/
开头。
因此,将 "auth/login"
更改为 "/auth/login"
即可。
关于ios - URLComponents.url 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39852659/