Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to infer complex closure return type with SwiftUI

Tags:

Following part 3 of Apple's tutorial on SwiftUI, Handling User Input, I get this error:

Unable to infer complex closure return type; add explicit type to disambiguate

I'm using the same code as the tutorial (even coping from the 'Complete' sample code doesn't resolve the error).

Does anyone have a cue what is going wrong here?

enter image description here

struct LandmarkRow: View {     var landmark: Landmark      var body: some View {         HStack {             landmark.image(forSize: 50)             Text(verbatim: landmark.name)             Spacer()              if landmark.isFavorite {                 Image(systemName: "star.fill")                     .imageScale(.medium)             }         }     } } 

Regardless of the cause of the issue, how could you indeed add an explicit type to disambiguate here? What would the return type in such a case be?

--

Update

Apparently you should not continue with your result of the 2nd tutorial with the 3rd. Some things changed in between of the tutorials that is not documented in the tutorial. I've added the project files to Github so you can check out the diff.

It's better to start the 3rd tutorial fresh with a fresh download of the Project files of the 3rd tutorial.

like image 855
Tieme Avatar asked Jun 04 '19 20:06

Tieme


2 Answers

The issue is not with the closure, but with the isFavorite property on landmark.

It is not declared on the Landmark type, and the compiler instead of showing the undeclared property error, unable to resolve the stacks build closure return type, so it shows and error there.

Great job Apple tutorial writers and even better one Xcode11 compiler.

To fix:

  1. Declare isFavorite variable on the Landmark type.
  2. Make sure you update the landmarkData.json for every landmark record with the isFavorite = false entry, otherwise the app will crash at runtime.
like image 61
dirtydanee Avatar answered Oct 17 '22 04:10

dirtydanee


Some background to this problem

Like @dirtydanee already answered there is a difference between those two tutorials. But the problem behind the problem is that while it looks like you're doing a configuration it's actually just functions nested in functions using generics and protocols to "magically" parse everything into a compiling function.

However conformance to these generics and protocols need to be pretty precise because if not the whole tree of functions cannot compile anymore. But it's hard to determine for the compiler what conformance actually failed. This is why you see an error at the top rather than at the point where it actually happens.

It's strongly advised to make sure your views are decomposed into natural and simple blocks so you're not pouring over hundreds of lines of View code to find that one bug.

Dave DeLong had a really great talk about how to compose Views from ViewControllers that still holds true until today: basically you never use View as a subview inside another View but you need to decompose your View of many, simple Views. Otherwise these errors'll drive you nuts.

like image 38
Lucas van Dongen Avatar answered Oct 17 '22 03:10

Lucas van Dongen