Member-only story

The Inner Workings of State Properties in SwiftUI

It’s not magic — even though it seems to be

Andrew Zheng
Better Programming
12 min readJun 17, 2020

Hey! Just to clarify, this article is about the @State property (unique to SwiftUI) and not the general concept of state. They’re related, but state itself is just an idea while the @State property is an actual, visible, and tangible object. It’s also very versatile, almost like magic …

Background

If you’ve been working with SwiftUI, chances are that you once made a property and tried to change its value, only to get this weird error: “Cannot assign to property: ‘self’ is immutable.”

Hmm … ‘self’ is immutable?

Above, pressedButton is a var, so its value is mutable (changeable) — but the error message says that “self” is immutable!

So pressedButton is mutable, but self isn’t? Let’s search up the error message.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Andrew Zheng
Andrew Zheng

Written by Andrew Zheng

WWDC21 Scholar. I like to read manga and write about Swift. Check out my app, Find — look for text in real life, with outrageous speed. getfind.app

No responses yet

What are your thoughts?