Day 9 of 100 Days of Swift

Days 1 to 12 of the “100 Days of Swift” course make up the “Introduction to Swift” section.

Day 9‘s access control, static properties, and laziness. The specific sections were: initializers, referring to the current instance, lazy properties, static properties and methods, and access control.

Thoughts about Day 9

Day 9’s lessons about access control and static properties in Swift was a review of concepts that I was already familiar with from Java. The keyword lazy that Swift uses to create a property only when the property is used for the first time was new to me.

I’ve been making better effort to concentrate on reading each line of code carefully for each example code given in the Tests before selecting my answer to a question. I still make the errors of not noticing:

  • when code is trying to modify a constant’s value
  • when a non-String value is trying to get assigned to a String property
  • when the character casing used for a parameter name doesn’t match with character casing used for the same name in the program.

My Day 9 notes taken from my paper notebook

  1. Initializers
    • memberwise initializer = all Swift structs get a synthesized memberwise initializer by default.
    • Swift automatically creates an initializer that accepts values for each of a struct’s properties.
    • if a struct’s property has a default value then the default value will be incorporated into the initializer as a default parameter value.
    • Swift REMOVES the memberwise initializer if you create an initializer of your own, using the keyword init().
  2. Referring to the current instance
    • keyword self refers to the struct’s properties to which the self is prefixed.
    • parameter names used by the initializer must be the same parameter name and type as the properties declared for the struct.
  3. Lazy properties
    • keyword lazy when placed before a property name’s type tells the struct to create the property only when the property is accessed for the first time.
    • the Optional reading contains a link to an external article that goes into more details about Swift’s lazy keyword.
  4. Static properties and methods
    • for an int() or func() inside a struct to use a static property the static property must be referred to using format <struct name>.<static property name>
  5. Access control
    • keyword private before a property’s type in a struct prevents the property’s value from being directly accessed from outside the struct.
    • keyword public before a property’s type or a function in a struct lets all other code use the property or function.
    • the Optional reading contains a link to Apple’s official documentation about Swift’s access control.
    • if the struct contains a private property then the struct must either assigned a value directly to the property or assign a value to the property from inside an init().
    • private properties must be initialized by an init() using the same spelling and casing as declared inside the struct.
    • don’t forget: Swift does not allow Int and Double properties to mix with each other. Kept overlooking this in the Test questions.

Today’s total study time: 3 hours

100 Days of Swift cumulative study time: 24 hours 45 minutes

[Note: those are actual study time values after subtracting break-time minutes from the Day’s study session]