问题
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