我想在Alamofire的响应回调中使用我的自定义委托方法,如下所示:
func startDownloadSegment() {
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let filePath = self.generateFilePath()
return (filePath, [.createIntermediateDirectories])
}
// 1
print(self.delegate)
Alamofire.download(downloadURL, to: destination).response { response in
// 2
print(self.delegate)
if response.error == nil {
self.delegate?.segmentDownloadSucceeded(with: self)
} else {
self.delegate?.segmentDownloadFailed(with: self)
}
}
}
如您所见,No.1 print(self.delegate)返回我设置的委托人。但No.2总是返回nil,因此无法调用downloadSucceeded(with :)等委托方法。
谢谢。
答案 0 :(得分:1)
我发现了问题。问题是我将委托设置为
弱var委托
但是就像在Alamofire的回应中一样,我应该省略“弱”的回复。完成它的关键字。