【问题标题】:"Cannot call value of non-function type 'HTTPURLResponse?'" (Alamofire 4.0) [Swift 3.0]“不能调用非函数类型 'HTTPURLResponse?' 的值”(Alamofire 4.0)[Swift 3.0]
【发布时间】:2017-01-22 21:09:45
【问题描述】:

我收到此错误:

"不能调用非函数类型'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!)
        }
}

【问题讨论】:

  • 不。类似的问题,但不一样。 swift 和 alamofire 的不同版本。
  • 您的目标是 iOS 8 吗?我不知道原因,但用于 Swift 3 的 Alamofire 必须至少是 iOS 9。问候
  • @JordanClark - 也许不同的版本,但问题是一样的。您的闭包参数不正确。
  • 顺便说一句,你的服务器真的发回了NSDictionary的plist吗?这有点不寻常。通常是 JSON 或类似的。

标签: swift alamofire swift3


【解决方案1】:

在 Alamofire 4 中,.response 方法采用带有单个参数 DefaultDownloadResponse 的闭包。

顺便说一句,如果您要使用自己的路径进行下载,我对您的return finalPath! 感到困惑,因为 Alamofire 4 期望您返回一个由文件 URL 和选项组成的元组,其中其中一个选项是.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)
    }
}

【讨论】:

    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多