Odd compiler error with using .assign() to a keypath

I’m working with Combine in Xcode 11 beta 3 and IOS 13 beta 3, and I’m hitting an odd compiler error. I’m not sure if I’m not specifying something sufficiently or completely, or if this is a error with the swift compiler not being able to correctly infer what it needs to know.

 

I have a UIImageView reference, and I’m making my code is a Combine pipeline that retrieves a URL to populate that imageview and then (tries to use) assign to set it into place.

 

The short form of this code (full code at https://github.com/heckj/swiftui-notes/blob/master/UIKit-Combine/ViewController.swift#L167-L231):

URLSession.shared.dataTaskPublisher(for: URL(string: firstUser.avatar_url)!)
                    // ^^ this hands back (Data, response) objects
                    .handleEvents(receiveSubscription: { _ in
                        DispatchQueue.main.async {
                            self.activityIndicator.startAnimating()
                        }
                    }, receiveCompletion: { _ in
                        DispatchQueue.main.async {
                            self.activityIndicator.stopAnimating()
                        }
                    }, receiveCancel: {
                        DispatchQueue.main.async {
                            self.activityIndicator.stopAnimating()
                        }
                    })
                    .map { $0.data }
                    // ^^ pare down to just the Data object
                    .map { UIImage(data: $0)!}
                    // ^^ convert Data into a UIImage with its initializer
                    .subscribe(on: self.myBackgroundQueue)
                    // ^^ do this work on a background Queue so we don't screw
                    // with the UI responsiveness
                    .catch { err in
                        return Just(UIImage())
                    }
            .receive(on: RunLoop.main)
            // ^^ and then switch to receive and process the data on the main
            // queue since we're messin with the UI
            .assign(to: .image, on: self.githubAvatarImageView)

 

This sequence of code results in the compiler error:

/Users/heckj/src/swiftui-notes/UIKit-Combine/ViewController.swift:166:26: error: type of expression is ambiguous without more context
             .assign(to: .image, on: self.githubAvatarImageView)
                         ^~~~~~~

 

If I instead use sink subscriber, then it applies just fine:

            .sink(receiveValue: { image in
                self.githubAvatarImageView.image = image
            })

 

Is there something I should be doing to make the express less ambiguous, or a means to writing this a different form of constructing the keypath that would resolve this?

Powered by WPeMatico

About

You may also like...

Comments are closed.