【问题标题】:invalid conversion from throwing function of type - swift 3类型的抛出函数的无效转换 - swift 3
【发布时间】:2017-08-05 13:50:36
【问题描述】:

我正在使用URLRequest,但出现此错误:

invalid conversion from throwing function of type...

我的代码:

func getFirstPageApplication(EMPTY:String,completionHandler: @escaping (_ response: AnyObject) -> ())
{
    var strResponse = "null"
    var request = URLRequest(url: URL(string: self.baseURL+"getFirstPageApplication")!)
    request.httpMethod = "GET"
    let postString = "EMAIL=\(EMPTY)"
    request.httpBody = postString.data(using: .utf8)
    let task = URLSession.shared.dataTask(with: request)
    { data, response, error in
        guard let data = data, error == nil else {                                                 // check for fundamental networking error
            print("error=\(error)")
            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        do {
            let json:AnyObject? = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            if let parseJSON = json{
                print(parseJSON)
            }
            completionHandler(parseJSON)
        }

    }
    task.resume()

}

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    试试这个:

      let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
                        if error != nil {
                            DispatchQueue.main.async {
                                response({} as AnyObject)
                            }
    
                        } else {
                            if let usableData = data {
                                do {
    
                                    let jsonResult = try JSONSerialization.jsonObject(with: usableData, options:
                                        JSONSerialization.ReadingOptions.mutableContainers)
                                    print(jsonResult) //this part works fine
                                    DispatchQueue.main.async {
                                        response(jsonResult as AnyObject)
                                    }
    
                                } catch {
                                    DispatchQueue.main.async {
                                        response({} as AnyObject)
                                    }
                                    print("JSON Processing Failed")
                                }
                            }
                        }
                    }
    

    【讨论】:

    • 我收到了这个错误:use of unresolved identifier callback{}
    • 您只需将块名称替换为您的块名称即可。
    • 我认为响应应该替换为completionHandler。是吗?
    • 是的..正确的。你只需要替换completionHandler。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多