【问题标题】:Swift - Missing argument for parameter 'host' in callSwift - 调用中参数'host'的参数丢失
【发布时间】:2015-02-17 23:00:34
【问题描述】:

这是我的代码:

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)

请问有什么办法解决这个问题吗?

谢谢!

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    该错误具有误导性。问题不在于缺少参数,而在于您如何访问NSURL。您要么必须添加 ? 才能访问可选,因为 NSURL 可以为空,要么您解开它:

    //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")
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-11
      • 2023-03-25
      • 2021-03-06
      • 1970-01-01
      • 2014-10-15
      • 2017-02-23
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      相关资源
      最近更新 更多