首页 > 解决方案 > 无法将类型“(_)->()”的值转换为预期的参数类型“CompletionHandler?” 使用 KingFisher Swift

问题描述

我正在使用一个名为 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?” (aka 'Optional<(Optional, Optional, CacheType, Optional) -> ()>')

标签: iosswiftkingfisher

解决方案


迅捷 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
    }
}

推荐阅读