|
Hi there, I'm working on Project 1. When Paul introducing "state" https://www.hackingwithswift.com/books/ios-swiftui/modifying-program-state He said
I'm confused about why he said " we can't change cumputed properties" . I mean, var computed properties are meant to change as far as its related parameters change, right ? It is a "var", In the following case, vacationRemaining will change, given a new vacationTaken value, with or without a "@State"
So why do we need "@State" to make computed property changeable ? |
|
You are confusing two different things: 1) computed properties, and 2) the Computed properties are only ever read-only. In something like this:
The Consider the following...
Since structs are value types (meaning the value itself is stored, as opposed to reference types, which store a pointer to the value), changing a property on them actually creates a whole new instance of the struct and replaces the original. But you can't replace the value of a As for the SwiftUI example, since |
|
@roosterboy Thank you so much for breaking it down in details for me. Very very clear ! I have read/thought about it for the whole night, and I also went back to watch Paul's tutorial a few times. I think I 98% understand it now... the Now I have a new questions: Are all pages we interact with on apps are "body (coumpted property)" ? I thought it was the Thanks Boat |
|
In SwiftUI, all GUI elements are produced by the If you aren't using SwiftUI, like you're using UIKit or AppKit instead, things work differently. But for SwiftUI, yes, everything comes from the computed |
|
Can't be clearer. Thanks |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.