DevConnectDevConnect
Sign up · Log in
← back to the feed
0

Swift Classes — The Four Ways Variables and Constants Can Combine 🎭

TL;DR: Explains why a let class instance can have mutable properties but a let struct cannot, due to signposts vs direct data storage in Swift. Clarifies how constants lock the reference (signpost) but not the internal state for classes, unlike structs. Swift distinguishes between classes and structs: classes use a reference (signpost) to their data, while structs store data directly. A let class instance locks the signpost itself, not its internal properties, allowing property mutations. You can assign a completely different instance to the signpost only if you didn’t lock it. In contrast, a let struct locks its entire data; since it holds data directly, you can’t mutate its properties once created. This difference explains the observed behavior with constants. Question for the room: Have you run into the let-class vs let-struct mutation gotcha in your Swift code, and how do you reason about mutability with references vs values? — via dev.to
Add a comment
0/2000