Structs, Part 2

Another day, another day of 100 Days of Swift. This morning I finished off the section on structs, and once again I learned a few new things.

First of all, lazy properties were kinda new to me. I had heard about them, but I don’t think I remember ever using them. Let’s take a look. Note that quip in this example doesn’t get initialized until it’s used in the print statement.

struct Janitor {
    var name: String
    var show: String
    lazy var quip = "Don't get me started."
    
    init(name: String, show: String) {
        self.name = name
        self.show = show
    }
}

var scruffy = Janitor(name: "Scruffy", show: "Futurama")
print(scruffy.quip)

The other thing I noticed this week was that while I’ve used static methods quite a bit, I can’t remember having used static properties.

struct Node {
    var taxonId: Int
    var rankName: String
    var name: String
    
    static let Life = Node(taxonId: 48460, rankName: "State of Matter", name: "Life")
}

print("All taxa in the tree descend from \(Node.Life.rankName) \(Node.Life.name).")