5

I add the following code to a VStack in SwiftUI:

KFImage.url(url)
     .placeholder(UIImage(named: "logo"))
     .setProcessor(processor)
     .loadDiskFileSynchronously()

But XCode doesn't like it and says "Cannot convert value of type UIImage? to expected argument type '() -> Content'"

Kingfisher's cheatsheet indicates that the placeholder line should work:

.placeholder(UIImage(named: "logo"))

When I remove the placeholder line (line2 above), it works properly. But, of course, there is no placeholder.

Using SwiftUI, how can I set the placeholder for KFImage?

I've only been coding with Swift/SwiftUI for a month now so I'm quite new.

Thankyou.

Swift Version 5.0 Kingfisher Version 6.1.0

AppyAndy
  • 53
  • 1
  • 5

1 Answers1

11

The error that you are getting is telling that placeholder wants a SwiftUI View (eg () -> Content) and you're trying to provide it a UIKit UIImage instead.

Instead, you should give it a SwiftUI Image.

You could easily initialize Image with a UIImage, but keep in mind that UIImage(named:) returns an Optional (which your error hinted at as well), so if you want to use it without checking it for nil first, you'll need to force unwrap it:

Image(uiImage: UIImage(named: "logo")!)

Even better, just initialize your SwiftUI Image with the name and skip UIImage altogether unless you need it for some other reason:

Image("logo")
.placeholder { Image("logo") }
jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Thankyou. That helped me solve the problem! It still wasn't working though, and it only started working when I used .placeholder { Image("logo") } instead of parenthesis. As in XCode still said Image wasn't acceptable until I used curly brackets instead of parenthesis. – AppyAndy Feb 01 '21 at 23:58
  • Nice -- I've edited my answer to reflect that in case others come in the future to see it. Since I see you're new here, please feel free to now upvote and accept the answer – jnpdx Feb 01 '21 at 23:59
  • 1
    Goodo. Yeah I did upvote it, but I don't have enough reputation for my vote to be publicly displayed. It's a thumbs up from me anyway. – AppyAndy Feb 02 '21 at 00:01