如何在数据提供程序类中设置未正确调用的协议函数 阿拉莫火函数 swift.



我需要在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

视图控制器代码:

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 之前初始化基类的属性。

最新更新