Skip to content

Latest commit

 

History

History

day-009

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Day 9: Structs, Part Two

Follow along at https://www.hackingwithswift.com/100/9.

📒 Field Notes

Initialzers

Initially, initializers for structs are handled by default: As long as parameters for every property are passed in to the constructor, Swift will run a memberwise initializer that assigns those parameters to the instance's matching properties.

This is separate from classes, which don't have a default memberwise initializer. But we can still define our own if we want. This can be useful when we want to setup our own default for a property — while also alleviating caller from being concerned with setting it.

Referring to the current instance

Anyone who's done object-oriented programming in other languages is likely familiar with this concept — and its various flavors of self, this, @, and I'm sure more.

While each language seems to have its own self-related nuances, self in Swift feels pretty well-thought out. It's not required — unless we're in a closure, or some block where a local variable has the same name as an instance member — so using it tactically can add more meaning to the code than using it everywhere. It's also not awkwardly, magically, implicitly crammed into to every method as the first argument — not that any language would ever do that, of course 😜.

Perhaps most importantly, though, it's the perfect word for what it is. I'll take self over this and @ any day.

Lazy properties

While I've known about the benefits of lazy initialization while programming in other languages, I never really found myself being as mindful of it while defining structs/classes/objects as I do in Swift.

I'd like to think some of that is simply due to learning — but credit where it's due: Swift's lazy keyword is a beautiful example of how clean, straightforward, and seamless implementations can actively encourage better developer practices and ways of thinking.

Static Properties

If you can think of a struct or a class definition as a stamp — from which new instances are printed out — you can probably think of static properties as a part of the handle that always stays put.

This can be useful for sharing/managing state across all instances of a type — and then selectively providing access to it through instance methods.

Access Control

Swift offers five levels of access control. From the official documentation:

  • Open access
  • Public access
  • Internal access
  • File-private access
  • Private access

The Swift documentation provides nice descriptions of each. On the whole, though, it seems both comprehensive and clearly named 👍.

🔗 Related Links