我正在使用一个名为KingFisher的图书馆从互联网上下载图像。供参考:
https://github.com/onevcat/Kingfisher
https://cocoapods.org/pods/Kingfisher
imageView.kf.setImage(with: url)
这个指令完美无缺,但是我想要跟踪成功,所以我添加了完成处理程序,所以文档建议这个片段。
imageView.kf.setImage(with: userInfo.getImageUrl()){ result in
switch result {
case .success(let value):
print("success")
case .failure(let error):
print(error) // The error happens
}
}作为参考,这是我正在使用的备忘单:
https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet
在添加此代码段时,我将得到以下编译错误:
无法将类型'(_) -> ()‘的值转换为预期的参数类型'CompletionHandler?’(又名‘Optional<(可选,可选,CacheType,可选) -> ()>')
发布于 2019-01-25 13:33:52
Swift 4.2翠鸟5.1
let url = URL(string: "https://example.com/high_resolution_image.png")
let imageView = UIImageView()
imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: nil) { result in
print(result)
switch result {
case .success(let value):
print("success")
print(value.source.url!)
case .failure(let error):
print(error) // The error happens
}
}发布于 2019-01-25 13:35:03
我使用的是KingFisher 5.1.0,我没有面对这样的错误。请再检查一下你的文件,
荚‘翠鸟’,'~> 5.1.0'
let url = URL(string: "your image url")!
self.kf.setImage(with: url) { result in
switch result {
case .success(let value):
print("Image: \(value.image). Got from: \(value.cacheType)")
case .failure(let error):
print("Error: \(error)")
}
}https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet
如果你使用的荚版本小于5.0,很明显,你最终会得到
Cannot convert value of type '(_) -> ()' to expected argument type 'CompletionHandler?' (aka 'Optional<(Optional, Optional, CacheType, Optional) -> ()>')发布于 2020-02-19 11:08:25
我也有过类似的问题。我一直在使用翠鸟4.0.0和Swift 4。
换到下面这招成功了,
imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: nil) { image, error, cacheType, imageURL in
}https://stackoverflow.com/questions/54365906
复制相似问题