Days 1 to 12 of the “100 Days of Swift” course make up the “Introduction to Swift” section.
Day 10‘s lesson covered classes and inheritance. The specific sections were: creating your own classes, class inheritance, overriding methods, final classes, copying objects, deinitializers, and mutability.
Thoughts about Day 10
Day 10’s lessons about classes and inheritance was familiar to me because of my past experience with the Java language. New to me in Day 10’s lesson was Swift’s deinit function that is used by classes when an instance of a class’s object is destroyed.
The background music for today’s study session was streamed from YouTube and titled “6 Hours Mozart for Studying, Concentration, Relaxation“.
My Day 10 notes taken from my paper notebook
- Creating your own classes
- classes ALWAYS require you to create an initializer for it if the class’s body is not empty.
- from the Optional reading: “…most Swift developers prefer to use structs rather than classes where possible.” Why is this? Ask this question in the “100 Days of Swift” forum on the Hacking With Swift site.
- Class inheritance
- from Optional reading: Paul wrote the following about Swift classes not automatically creating initializers, “…Swift classes don’t have this feature [synthesized memberwise initializer] which is annoying.” I think opposite of Paul about this because I’d rather manually create my own class initializer instead of it being created automatically. Reason is because I have more programmatic control of the class.
- Overriding methods
- keyword override used in a sub-class to override the functionality of a method inherited from its parent class helps the overridden function look visually obvious that it overrides its inherited behavior. I like how Swift makes overriden functions/methods look obvious, unlike how not-so-obvious it is in Java.
- note from Test question 12: remember that unless you specify to print (using the print() function) of text returned by a function, the returned text will not be displayed/printed.
- Final classes
- keyword final placed before keyword class disallows other developers from building classes based on the class. Stated another way, final classes CANNOT be inherited from.
- Copying objects
- an instance of a class is like a TV remote control used to control an instance of a TV.
- a copy of an instance of a class is like a second copy of a TV remote control used to control the same instance of the TV that is controlled by the first TV remote control.
- the Optional reading has a link to an article/post about “structs and mutations in Swift”, which I decided to read for a future time.
- Deinitializers
- the deinit funtion does NOT use parentheses after its name ( e.g. deinit() ), unlike init().
- attaching parentheses to the deinit function causes a syntax error.
- ARC = automatic reference counting.
- ARC tracks how many instances of each class exists.
- when the ARC value for a class = 0, Swift call the class’s deinit function and destroys the class’s object.
- the deinit function can be placed anywhere you want it inside the class. Recommend putting the deinit function at the bottom of the class.
- Mutability
- no notes recorded.
- Classes summary
- Swift supports inheriting from ONLY ONE class.
Today’s total study time: 2 hours 45 minutes
100 Days of Swift cumulative study time: 27 hours 30 minutes
[Note: those are actual study time values after subtracting break-time minutes from the Day’s study session]