gpt4 book ai didi

ios - Swift - 调用中参数 'host' 缺少参数

转载 作者:可可西里 更新时间:2023-11-01 01:42:21 29 4
gpt4 key购买 nike

这是我的代码:

func submitLacunaRequest (#module: String, method: String, parameters: AnyObject, completion: (responseObject: AnyObject!, error: NSError!) -> (Void)) -> NSURLSessionTask? {
let session = NSURLSession.sharedSession()
let url = NSURL(string: "https://us1.lacunaexpanse.com").URLByAppendingPathComponent(module)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json-rpc", forHTTPHeaderField: "Content-Type")

调用中缺少参数“host”的参数。它发生在这条线上。

 let url = NSURL(string: "https://us1.lacunaexpanse.com").URLByAppendingPathComponent(module)

有什么解决办法吗?

谢谢!

最佳答案

该错误具有误导性。问题不是缺少参数,而是您如何访问 NSURL。您要么必须添加一个 ? 来访问可选的,因为 NSURL 可以为 null,要么您解包它:

//First option:
let url = NSURL(string: "https://us1.lacunaexpanse.com")?.URLByAppendingPathComponent(module)

//Unwrapping
if let url = NSURL(string: "https://us1.lacunaexpanse.com")?.URLByAppendingPathComponent(module){
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json-rpc", forHTTPHeaderField: "Content-Type")
}

关于ios - Swift - 调用中参数 'host' 缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28572835/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com