Will
Will

Reputation: 5470

generic parameter 'FalseContent' could not be inferred

I'm trying to create a 4x4 grid of images, and I'd like it to scale from 1 image up to 4.

This code works when the images provided come from a regular array

var images = ["imageOne", "imageTwo", "imageThree", "imageFour"]

However it does not work if the array comes from an object we are bound to:

@ObjectBinding var images = ImageLoader() //Where our array is in images.images

My initialiser looks like this:

init(imageUrls urls: [URL]){
    self.images = ImageLoader(urls)
}

And my ImageLoader class looks like this:

class ImageLoader: BindableObject {
    var didChange = PassthroughSubject<ImageLoader, Never>()

    var images = [UIImage]() {
        didSet{
            DispatchQueue.main.async {
                self.didChange.send(self)
            }
        }
    }

    init(){

    }

    init(_ urls: [URL]){
        for image in urls{
            //Download image and append to images array
        }
    }
}

The problem arises in my View

var body: some View {
    return VStack {
        if images.images.count == 1{
            Image(images.images[0])
                .resizable()
        } else {
            Text("More than one image")
        }
    }
}

Upon compiling, I get the error generic parameter 'FalseContent' could not be inferred, where FalseContent is part of the SwiftUI buildEither(first:) function.

Again, if images, instead of being a binding to ImageLoader, is a regular array of Strings, it works fine.

I'm not sure what is causing the issue, it seems to be caused by the binding, but I'm not sure how else to do this.

Upvotes: 1

Views: 2706

Answers (1)

kontiki
kontiki

Reputation: 40519

The problem is your Image initialiser, your passing a UIImage, so you should call it like this:

Image(uiImage: images.images[0])

Note that when dealing with views, flow control is a little complicated and error messages can be misleading. By commenting the "else" part of the IF statement of your view, the compiler would have shown you the real reason why it was failing.

Upvotes: 3

Related Questions