“Cannot call value of non-function type 'HTTPURLResponse?'” (Alamofire 4.0) [Swift 3.0]

柔情痞子 提交于 2019-12-07 03:17:53

问题


I'm receiving this error:

"Cannot call value of non-function type 'HTTPURLResponse?'"

on the section:

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

and I was wondering if anyone can help me with this.

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:


In Alamofire 4, the .response method takes a closure with a single parameter, the DefaultDownloadResponse.

By the way, if you're going to use your own path for the download, I'm confused by your return finalPath! because Alamofire 4 expects you to return a tuple consisting of the file URL and the options, of which one of the options is .removePreviousFile, which saves you from manually removing it yourself.

For example:

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


来源:https://stackoverflow.com/questions/39500976/cannot-call-value-of-non-function-type-httpurlresponse-alamofire-4-0-swi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!