【发布时间】:2022-08-04 23:17:32
【问题描述】:
我实现了以下代码,我可以在其中传递资源的名称,它应该给我 URL。我正在使用 Xcode 14 Beta 3。
static let baseUrl = \"localhost:8080\"
static func resource(for resourceName: String) -> URL? {
var components = URLComponents()
components.scheme = \"http\"
components.percentEncodedHost = baseUrl
components.path = \"/\\(resourceName)\"
return components.url
}
我将资源名称传递为\'my-pets\',它应该返回http://localhost:8080/my-pets,但它一直返回http://my-pets。我不确定我在哪里犯了错误。
-
我在 Playgrounds 中测试了你的代码并得到了
http://localhost:8080/my-pets -
谢谢!也许与 Xcode 14 Beta 3 有关。
-
components.url应该返回nil,因为在您的情况下,\"host\" 在语法上是不正确的(请参阅 RFC 3986 中的 \"3.2.2 \"Host\")。对于方案 \"http\" 是一个 URI当它没有主机时是无效的。因此,URLComponents 作为 URL 返回的内容是无效的。您可以提交错误。:)
标签: swift xcode nsurlcomponents xcode14