I can’t understand this SwiftUI compile error.

Text("Sample Text with wrong font type")
     .font(.bold)

I know this code is incorrect, and it returns an error stating that .font doesn’t have a .bold type.

However, when I place this incorrect code within a stack…

…no error message appears. Instead, I get:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

when trying to run the code. It’s unclear why the compiler doesn’t specify that the issue originates from .font(.bold). This was confusing, and I spent 20 minutes figuring out it was a typo.

I've had this a few times, but the message says the compiler is unable to type-check it in a reasonable time, which AFAIK - is why you can't see where the error is: it took too long to figure it out.

That is a frustrating issue with SwiftUI, how poor and misleading the error reporting may be. That is one of the reason I'm not very fond of SwiftUI…

My understanding (but I may be wrong) is that when the error occurs at top level of the view, it is correctly matched.

But when inside s structure (List, ForEach or whatever), compiler just fails on this part ; and at higher level (body), get noticed that a subview was not created. And it guesses it is because it is too complex.

When I get such error, I work by dichotomy:

  • comment out a significant part (about half) of the body code
  • if still fails, then error is in the other part ; so I comment out half of this other part
  • if is works, then I uncomment half of the commented and test again…

That's the best I've found.

another approach is to paste the offending code into some well-known llm, and ask it "what is wrong with this SwiftUI code".

A predictive text model is quite good at spotting this kind of error, and suggesting a correction.

I can’t understand this SwiftUI compile error.
 
 
Q