简体   繁体   中英

Ambiguous use of operator '<~' in ReactiveSwift

I inherited a somewhat old project written in ReactiveSwift (v3.1.0) and ReactiveCocoa (v7.2.0). When I opened it on Xcode 10.1, it was built without an issue. But when I did the same on Xcode 10.3, I get the error Ambiguous use of operator '<~' on the following block of code.

self.newMatchesTitleLabel.reactive.text <~ self.viewModel.newMatchesViewModel.data.producer.map { matches in
    let newMatchesCount = matches.filter({ !$0.hasViewedOnce }).count
    let newMatchesString = matches.count == 1 ? "New Match" : "New Matches"
    return newMatchesCount == 0 ? newMatchesString : "\(newMatchesString) (\(newMatchesCount))"
}

It seems to be because of all the variables and calculations done inside the closure, the compiler somehow fails to infer the type(?). When I comment out those parts, compiler shuts up. But obviously I need these to work. Is there a way to fix this just the way it is?


I attempted taking out those parts out of the closure by declaring two class level variables.

self.newMatchesCount <~ self.viewModel.newMatchesViewModel.data.producer.map { $0.filter({ !$0.hasViewedOnce }).count }
self.newMatchesString <~ self.viewModel.newMatchesViewModel.data.producer.map { $0.count == 1 ? "New Match" : "New Matches" }

What I can't figure out now is how to the final boolean check and assign that value to the newMatchesTitleLabel the reactive way.

I'm not well-versed in ReactiveSwift/ReactiveCocoa so I might be making it worse. And updating the entire project to use the latest versions is out of the question at this point unfortunately. I just need this error to go away.

This is not the solution, but rather some steps for you to try to find a fix.

The Swift compiler doesn't always give meaningful error messages when closures are involved, in many times even giving completely unrelated errors. This happens mostly with closures that are not explicitly declared - eg by fully declaring the parameter types and the parameter result.

In your code I see two closures, so the first step would be to add the type declarations to them

self.viewModel.newMatchesViewModel.data.producer.map { (matches: TypeForMatches) -> ResultType in

, and

let newMatchesCount = matches.filter({ (match: MatchType) -> Bool in !$0.hasViewedOnce }).count

Hopefully the compiler will give a more informative error message, or even better be happy as there's no more ambiguity and can pick up the right operator overload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM