It’s a pretty safe bet that if Xcode is saying there’s an error in my code, that it’s correct, and I am in error – not Xcode. Today I came across a situation where that might not be true.
I think the purple warnings are problems detected at runtime – I’ve heard of thread problems causing purple warnings. The error I was getting was “Publishing changes from within view updates is not allowed, this will cause undefined behavior.“
The error was on two lines in my model where I’d called a method to update the model from a button press in a sub-view function.

After poking around to try and work it out, I found this clear blog post from Donny Wals and it turns out it’s possibly a bug in XCode 14. My situation fitted the description – calling the method from a button in a list, and the temporary workaround of adding a modifier to the button eliminated the warning.
I’m on Xcode 14.0, and I gather from Donny’s post that the problem might have been fixed in newer versions.
Here’s the calling code – a button in a list, with the “fix” on line 25.
Leave a comment