Stephen DeStefano Swift 5 Course - Part 4 (Lectures 12 to 16)
Not going to go into rambling detail about how it's been a long time etc. End of term, start of holiday is always mental! I have missed my regular coding so am going to get some in today, then there will be more on Monday! I was working through Stephen's course, which was becoming pretty technical and challenging. Let's go!
Start Time - 07:17
Stored Properties
So these store constant/variable data.
A key point here. Structs are value types - if you have a constant stored property, you cannot change the value. You can for classes as they are reference types.
All that was pretty easy to be honest.
Lazy Properties
So I'm just trying to figure out the purpose of these! Stephen is quite rambling.
OK so when the property is actually initialised, that's when the lazy stored property is accessed. It's about saving memory. So not created until it has actually been used.
If it's computationally expensive - if it will end up slowing things down! Right, I get it.
Another good use is when outside factors are not known until init of that instance is done. E.g. if one of the properties is of a custom class and requires some computation.
Computed Properties
Haven't got huge amounts more so am going to crack on with these now.
This is to do with getters and setters -

Area is the computed property. It needs some sort of computation (using the stored properties).
It behaves a bit like a function - no value is stored inside there. It runs a calculation.
You don't need the get word. I prefer to leave it in.
This is a READ-ONLY property. Can't be set to a different value.
Set - not essential, also optional.

The set keyword is used; the newArea is the name put in for the newValue.
Property Observers
These are more about observing changes. The willSet and didSet keywords. Watching for when properties are set - only for var properties. Not for lazy stored properties.

So if the value is being changed, the willSet messages comes up. Then when it's changed, the didSet message appears. This is more for the code creator, rather than the person using the app!
If you don't want to put in the custom names, then newValue, oldValue are used.
Custom names included -

Good for a game when the scores are being updated!!
Static Properties
Last bit for now! Static keyword is used. It is attached to a type. So with the static keyword, that property will not be available.
So they belong to the type, no to the instance of that type. Not sure when that will be of use to be honest.
Override - used to override in the subclass something computed. Not sure how that's relevant to static!
Finish Time - 08:09 (52 minutes total)
Cool! So we are done with properties. A good way to 'ease back in!'. It's important not to get too bogged down with the technicalities with Swift. With context and practice, all of the above will make more sense. And it kind of already does! I am going to keep doing bits and pieces every day, or every other day at the worst just to keep things ticking along, before deciding how to get my learning onto the next level!
Start Time - 07:17
Stored Properties
So these store constant/variable data.
A key point here. Structs are value types - if you have a constant stored property, you cannot change the value. You can for classes as they are reference types.
All that was pretty easy to be honest.
Lazy Properties
So I'm just trying to figure out the purpose of these! Stephen is quite rambling.
OK so when the property is actually initialised, that's when the lazy stored property is accessed. It's about saving memory. So not created until it has actually been used.
If it's computationally expensive - if it will end up slowing things down! Right, I get it.
Another good use is when outside factors are not known until init of that instance is done. E.g. if one of the properties is of a custom class and requires some computation.
Computed Properties
Haven't got huge amounts more so am going to crack on with these now.
This is to do with getters and setters -

Area is the computed property. It needs some sort of computation (using the stored properties).
It behaves a bit like a function - no value is stored inside there. It runs a calculation.
You don't need the get word. I prefer to leave it in.
This is a READ-ONLY property. Can't be set to a different value.
Set - not essential, also optional.

The set keyword is used; the newArea is the name put in for the newValue.
Property Observers
These are more about observing changes. The willSet and didSet keywords. Watching for when properties are set - only for var properties. Not for lazy stored properties.

So if the value is being changed, the willSet messages comes up. Then when it's changed, the didSet message appears. This is more for the code creator, rather than the person using the app!
If you don't want to put in the custom names, then newValue, oldValue are used.
Custom names included -

Good for a game when the scores are being updated!!
Static Properties
Last bit for now! Static keyword is used. It is attached to a type. So with the static keyword, that property will not be available.
So they belong to the type, no to the instance of that type. Not sure when that will be of use to be honest.
Override - used to override in the subclass something computed. Not sure how that's relevant to static!
Finish Time - 08:09 (52 minutes total)
Cool! So we are done with properties. A good way to 'ease back in!'. It's important not to get too bogged down with the technicalities with Swift. With context and practice, all of the above will make more sense. And it kind of already does! I am going to keep doing bits and pieces every day, or every other day at the worst just to keep things ticking along, before deciding how to get my learning onto the next level!
Comments
Post a Comment