我正在尝试用它URLComponents来组成一个URL看起来像它的东西.
但是,当我访问url组件的属性时,它是零.
例...
var urlComponents = URLComponents(string: "http://google.com")!
urlComponents.path = "auth/login"
Run Code Online (Sandbox Code Playgroud)
然后我做......
print(urlComponents)
Run Code Online (Sandbox Code Playgroud)
输出...
scheme: http host: google.com path: auth/login
- scheme : "http"
- host : "google.com"
- path : "auth/login"
Run Code Online (Sandbox Code Playgroud)
但是之后...
print(urlComponents.url)
Run Code Online (Sandbox Code Playgroud)
输出nil.
我做错了吗?如何从这一切中获取完整形成的URL?谢谢
The doc of URLComponents.init(url:resolvingAgainstBaseURL:) says:
Returns the initialized URL components object, or nil if the URL could not be parsed.
Knowing that:
我假设URLComponents当 URL 符合 RFC 1808/1738/2732 但不符合 RFC 3986 时, 的初始化将失败。那是什么样的 URL?有什么例子吗?
到目前为止,我唯一的提示可能与不同的保留字符有关?
我实现了以下代码,我可以在其中传递资源名称,它应该为我提供 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
}
Run Code Online (Sandbox Code Playgroud)
我将资源名称传递为“my-pets”,它应该返回,http://localhost:8080/my-pets但它不断返回http://my-pets。我不确定我在哪里犯了错误。
将字符串解析为NSURL对象时,NSURL使用单个正斜杠将字符串处理为与方案后面带有双正斜杠的字符串不同.
为什么会这样?
以下是一些例子:
NSURL *url = [NSURL URLWithString:@"app-id://path1/path2"];
NSLog(@"%@",url.scheme); // "app-id"
NSLog(@"%@",url.path); // "/path2"
NSLog(@"%@",url.host); // "path1"
NSURL *url = [NSURL URLWithString:@"app-id:/path1/path2"];
NSLog(@"%@",url.scheme); // "app-id"
NSLog(@"%@",url.path); // "/path1/path2"
NSLog(@"%@",url.host); // nil
Run Code Online (Sandbox Code Playgroud) 我正在构建一项调查,当用户回答问题时,我将答案附加到带有参数的 url 字符串中,并向我的服务器发送 get 请求。因此,对于每个答案,都会记录所选答案、时间戳和调查的唯一 ID。
我不确定最好的方法,但这就是我到目前为止所拥有的。
我创建了 url 和查询项。
var urlComponents: URLComponents {
let resultID = surveyQuestions.resultId
print("\(String(describing: resultID))")
let resultResponseID = surveyQuestions.questions[surveyResultResponseId]
print("\(String(describing: resultResponseID))")
let questionIndex = questionNumbers
print("\(String(describing: questionIndex))")
var urlComponents = URLComponents(string: "My String URL")
urlComponents?.queryItems = [
URLQueryItem(name: "surveyResultsId", value: "\(String(describing: resultID))"),
URLQueryItem(name: "surveyResultsResponseId", value: "\(String(describing: resultResponseID))"),
URLQueryItem(name: "questions", value: "\(questionIndex)"),
URLQueryItem(name: "selectedAnswer", value: "\(storedAnswer)")
]
let url = urlComponents?.url
print(url!.absoluteString as Any)
return urlComponents!
}
Run Code Online (Sandbox Code Playgroud)
然后我构建发送请求。
func sendRequest(_ url: String, parameters: [String: String], completion: …Run Code Online (Sandbox Code Playgroud) 我正在尝试URLComponents()在我设计的应用程序中组成一个代表。
这是代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var components = URLComponents()
components.scheme = "http"
components.host = "0.0.0.0"
components.port = 9090
let queryItemToken = URLQueryItem(name: "/predict?text", value: "what's your name?")
components.queryItems = [queryItemToken]
print(components.url as Any)
}
}
Run Code Online (Sandbox Code Playgroud)
这是上述代码段的输出:
Optional(http://0.0.0.0:9090?/predict?text=what's%20your%20name?)
Run Code Online (Sandbox Code Playgroud)
由于 ? 在端口和查询之间!我怎样才能防止URLComponents()插入这个多余的?在端口和查询之间!
目标输出: Optional(http://0.0.0.0:9090/predict?text=what's%20your%20name?)
使用URLComponents,有没有办法避免主机的百分比编码?
var components = URLComponents()
components.scheme = "https"
components.host = "endpoint.com/v1/api.php?"
// ends up as https://endpoint.com%2Fv1%2Fapi.php%3F? but I want it to stay as https://endpoint.com/v1/api.php?
Run Code Online (Sandbox Code Playgroud)
这个问题是关于避免编码,而不是添加编码作为作为重复状态链接的问题。