这是我的代码:
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)
任何想法如何解决这个问题?
谢谢!
答案 0 :(得分:2)
这种错误令人误解。问题不是缺少参数,而是您如何访问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")
}