“不能调用非函数类型的值'HTTPURLResponse?'”(Alamofire 4.0)[Swift 3.0]

时间:2016-09-14 23:11:00

标签: swift alamofire swift3

我收到此错误:

  

“无法调用非函数类型的值'HTTPURLResponse?'”

部分:

.response { (request, response, data, error)

我想知道是否有人可以帮助我。

Alamofire.download(urlToCall, method: .get) { temporaryURL, response in
    if FileManager.default.fileExists(atPath: finalPath!.path!) {
        do {
            try FileManager.default.removeItem(atPath: finalPath!.path!)
        } catch {
            // Error - handle if required
        }
    }
    return finalPath!

    }
    .response { (request, response, data, error) in

        if error != nil {

        }
        var myDict: NSDictionary?
        let path = finalPath!
        myDict = NSDictionary(contentsOf: path)

        if let dict = myDict {
            success(dict)
        }

        if finalPath != nil {
            //doSomethingWithTheFile(finalPath!, fileName: fileName!)
        }
}

1 个答案:

答案 0 :(得分:0)

在Alamofire 4中,.response方法使用单个参数DefaultDownloadResponse进行闭包。

顺便说一句,如果您要使用自己的路径进行下载,我会对您的return finalPath!感到困惑,因为Alamofire 4希望您返回由文件网址组成的元组以及其中一个选项为.removePreviousFile的选项,可以避免您自己手动删除它。

例如:

Alamofire.download(urlToCall, method: .get) { temporaryURL, response in
    let finalURL: URL = ...

    return (destinationURL: finalURL, options: .removePreviousFile)
}.response { response in
    if let error = response.error {
        success(nil)
        return
    }

    if let fileURL = response.destinationURL, let dictionary = NSDictionary(contentsOf: fileURL) {
        success(dictionary)
    } else {
        success(nil)
    }
}