swift - "Cannot call value of non-function type ' HTTPURL 响应? '"(Alamofire 4.0) [Swift 3.0]

标签 swift alamofire swift3

我收到此错误:

"Cannot call value of non-function type '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!)
        }
}

最佳答案

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

关于swift - "Cannot call value of non-function type ' HTTPURL 响应? '"(Alamofire 4.0) [Swift 3.0],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39500976/

相关文章:

ios - 变量未更新

ios - 用户不会显示在 Firebase 的“身份验证”选项卡上

ios - 为什么 nib 文件中的 UITableViewCell(加载并添加到堆栈)在 viewDidAppear 中为零,但在 viewDidLoad 中没问题?

swift - Swift 中类型方法返回值的“未解析标识符”

ios - 如果使用 "ActiveLabel.swift"标记单词有空格,它将标记失败 - swift

swift - 网址中的空格 - alamofire

arrays - 为 Alamofire POST 请求 Swift 4 创建主体

ios - 如何在 Swift Alamofire 中使用 ssl 证书?

ios - 注册 UITableViewCell 不工作

iOS/ swift : directly manipulate remote MySQL over SSH