本文介绍了URLComponents.url为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用URLComponents组成一个URL,因为这似乎是它的用途.

I'm trying to use URLComponents to compose a URL as that appears to be what it's for.

但是,当我随后访问组件的url属性时,它为nil.

However, when I then access the url property of the components it is 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?谢谢

Am I doing something wrong with this? How do I get the fully formed URL from all this? Thanks

推荐答案

看来path参数的字符串必须以/开头.

It looks like path parameter's string must start with /.

因此将"auth/login"更改为"/auth/login"即可.

这篇关于URLComponents.url为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 20:51