我需要在Alamofire下载功能中设置协议功能以跟踪和观察进度分数值。我必须尝试实现委托功能,但不能正确执行给出错误。我有一个具有Alamofire函数的DataProvider类,然后在ViewController中调用它。
在init(webService: DataProvider = DataProvider())
上出现错误:
'self' used before 'super.init' call
'super.init' isn't called on all paths before returning from initializer
ViewController代码:
let webService: DataProvider
init(webService: DataProvider = DataProvider()) {
//super.init()
self.webService = webService
self.webService.delegate = self
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
extension DownloadViewController: MyWebServiceProtocol {
func progress(_ fractionCompleted: Double) {
print(fractionCompleted)
}
func downloadDidSucceed() {
print("download")
}
func downloadDidFail(error: Error) {
// handle error
}
}
DataProvider.Class
protocol MyWebServiceProtocol: class {
func progress(_ fractionCompleted: Double)
func downloadDidSucceed()
func downloadDidFail(error: Error)
}
// in class
weak var delegate: MyWebServiceProtocol?
//Alamofire
Alamofire.download(
url,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: { (progress) in
//progress closure
self.delegate?.progress(progress.fractionCompleted)
print(progress.fractionCompleted)
}).response(completionHandler: { (DefaultDownloadResponse) in
//here you able to access the DefaultDownloadResponse
//result closure
callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.absoluteString.replacingOccurrences(of: "file://", with: ""))
print(DefaultDownloadResponse)
self.delegate?.downloadDidSucceed()
})
最佳答案
您应该在使用self对象之前调用super.init()
,以便在使用self之前初始化基类的属性。
关于ios - 如何设置协议(protocol)函数不能在DataProvider类中正确调用Alamofire函数swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59764565/