|
Hi , I understand that in Does that mean , by default, all the instances created from this Struct will have these static properties/methods (without initializer). Because I imagine: if I change @static propoerties from a Struct level, it will automatically that property across all instances from that Struct. In Paul's code it is like this:
I guess, by default, from this moment on, all instances created from this Struct will naturally have "Taylor Swift " in their property , and the stuentCount will by be 1 already. ? Boat |
|
Nope. Static properties belong to the type, not any particular instance of that type. So, you can do this:
But you can't do this:
In those examples, Note that this is one reason why following the Swift convention of naming types with uppercase (e.g., |
|
But what is the point of having properties and methods accessing to the type (Struct) only without any impact on its instances ? I mean, a Struct is not of any use if there is no instance created from it, right ? |
|
I think the usefulness is being able to keep information organised together in one thing (Struct in this case). You can keep both instance information (non-static properties and methods) and "global" information (static properties and methods) that pertain to the same concept (e.g. a Student or an Employee or a Car, etc.) in the same, single, place. Also, remember that you can access the static information from the non-static properties and methods. I like the idea of having instances keep track of individual data and using static properties/methods to accumulate statistics about all instances. Seems like a nice way to organise things. |
|
You can still access those static properties and methods inside a struct instance, but they don't belong to the instance. Paul's example perhaps isn't the best example to demonstrate, but you can do stuff like this:
So individual instances of Static properties can also be useful when you have something like a
So individual instances of |
|
I've read through, think I kind of getting it...But I need to re read a few tims and spend more time to let it sink in. It sounds a bit like Thanks guys~ |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.