“无法调用非功能类型'HTTPURLResponse的值?'" (Alamofire 4.0)[Swift 3.0] [英] "Cannot call value of non-function type 'HTTPURLResponse?'" (Alamofire 4.0) [Swift 3.0]

查看:155
本文介绍了“无法调用非功能类型'HTTPURLResponse的值?'" (Alamofire 4.0)[Swift 3.0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

无法调用非功能类型'HTTPURLResponse的值?'

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

在该部分:

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

推荐答案

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

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

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

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.

例如:

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

这篇关于“无法调用非功能类型'HTTPURLResponse的值?'" (Alamofire 4.0)[Swift 3.0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆