ios - URLComponents.url 为零

标签 ios swift nsurl nsurlcomponents

我正在尝试使用 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/

相关文章:

ios - 如何从 react native 页面快速打开现有的ViewController?

swift - 如何在 Swift 中创建唯一对象列表数组

ios - 从包含 HTML 的 NSString 创建本地 NSURL?

IOS 缺少一些切换引用或其他东西?

ios - 有点复杂的自定义 UITableViewCell

ios - 正确更改按钮状态和属性。

swift - 无法使用 NSURLRequest 类型的参数列表调用 init

iOS 8 : Get title of song being played by the system music player

swift - 无法在 Swift 4 中为 WebKit View 创建 IBOutlet

swift - 如何使用 Swift 通过传递 URL 中的参数来上传图片?