Jor*_*ark 6 swift alamofire swift3
我收到此错误:
"无法调用非函数类型的值'HTTPURLResponse?'"
在该部分:
.response { (request, response, data, error)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有人可以帮助我.
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!)
}
}
Run Code Online (Sandbox Code Playgroud)
在 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)
}
}
Run Code Online (Sandbox Code Playgroud)