Search code examples
iosswiftkingfisher

Cannot convert value of type '(_) -> ()' to expected argument type 'CompletionHandler?' using KingFisher Swift


I am using a library called KingFisher to download image from internet. For the reference:

https://github.com/onevcat/Kingfisher

https://cocoapods.org/pods/Kingfisher

imageView.kf.setImage(with: url)

This instruction works flawless but i wanted to track success so i added completion handler so the documentation suggestion this snippet.

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

For the reference this is the cheat sheet i am using:

https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet

On adding this snippet i am getting this compilation error:

Cannot convert value of type '(_) -> ()' to expected argument type 'CompletionHandler?' (aka 'Optional<(Optional, Optional, CacheType, Optional) -> ()>')


Solution

  • Swift 4.2 Kingfisher 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
        }
    }