Learn Courses My Dashboard

Syntax problem, problem with Xcode 13.2 error reporting

I have a twofold problem here. In some code that is attached to a Button SwiftUI element, I appear to have a syntax error, but I can’t see it. The code is fine (apparently), compiles and runs, but as soon as I add a statement that copies a string, I get errors, but in the wrong place. This is a recurring problem that I’m having with SwiftUI (the inaccurate reporting of an error) – if there is a bug in the code, it is reported, not on the line with the error, but on the element that encloses the code. I have attached two screenshots showing this problem with the error reporting.

So, two questions – first, on the matter of this inaccurate error reporting, I am using Xcode 13.2, but I see that the most recent release of Xcode is 13.3. Upgrading to that is a bit of a pain point, does anyone know if 13.3 fixes the error reporting?

Secondly, I have no idea why taskList = "Happy" would be causing an error. It looks like a simple assign to me.


In the first picture, we have our error. In the second picture, I comment out the string assignment lines and no more error.

What’s really going on here?

You’re using a ForEach That is used for making views, see here as opposed to a control flow for loop, shown here

The ForEach loop you’re using is expecting to be building a SwiftUI View

Rather than the code you have inside the button really is more of a control flow kind of code

Either way yes this is a problem with SwiftUI in general because it is still very new

I would recommend upgrading to Xcode 13.3 because you should always be on the latest version for security reasons and such

You can do this via the App Store or from the Apple developer website

That’s fixed it (going from ForEach to a for loop), thank you.

Still unhappy with the way that Xcode tells you that there’s a problem, but doesn’t bother to tell you what the problem actually is. It means having to scrutinize code for unreported or syntax errors. Hopefully Apple will get that sorted out, the current state of Xcode makes it very difficult to develop using SwiftUI.

To be fair, once you understand how SwiftUI works the error messages that are displayed do make sense so perhaps it’s a little premature to be criticising the way Xcode works when you are new to SwiftUI.