100 Days of SwiftUI

Day 12

2026-04-29

Looks like it’s going to be a very full Day 12. I’m in the flailing part now. I think it will be easier once we’re actually doing something. FLW*

Classes

just because a feature exists, it doesn’t mean you need to use it

  • SwiftUI uses structs for UI design and classes for data

How to create your own classes

  • Classes have some similarities to structs (e.g. creating, properties and methods, custom inits)
  • Main differences
    1. Inheritance — make one class build on the components of another class
    2. Initializer — required to make your own (or use defaults)
    3. Copies — a copy of an instance of a class has the same data as the original instance. Change data in one, it changes them all
    4. Deinialize — used to free up reserved resources when the last instance is destroyed
    5. Constant class instances can have their variable properties changed

SwiftUI uses the Copies functionality a lot.

When you choose a class over a struct you’re doing so because you want one of the above behaviors

How to make one class inherit from

  • Terminology: parent/child or super/subclass
  • Syntax: Developer: Employeechild: parent
  • child can use parent methods, and can also have their own version of it using the override keyword. Sometimes you don’t need the override keyword because of course you don’t.
  • final keyword will stop the inheritance path

How to add initializers for classes

  • child classes must initialize its properties (if it has them) before it calls the parent’s initializer
  • call the parent’s initializer using super.init(...)
  • super isn’t just for initializers
  • if the child doesn’t have any initializers then it automatically inherits from its parent

How to copy classes

  • classes are reference types — they all refer to the same data
  • “It might seem like a bug, but it’s really a feature” when showing that 2 copies of an instance share the same data
  • structs do not share data, so changing the copy doesn’t change the original instance
  • you have to do some machinations to make independent instances from a copy of a class

How to create a deinitializer for a class

  • deinit used within the class structure, no parentheses (init(...) {} vs deinit {})
  • optional
  • don’t take parameters or return data
  • aren’t called directly (only by the system)
  • something something scope (the context in which things exist)
Scope is going to bite me in the butt a lot I bet

How to work with variables inside classes

  • if a property of a class is variable, even a constant instance can change that variable. The pointer is going to the same location in memory, but the content of that location has changed. The instance still exists.

  • Given this example there are four options:

class User {
    var _or let_ name = "Paul"
}

let _or var_ user = User()
user.name = "Taylor"
print(user.name)
  1. Constant instance, constant property – a signpost that always points to the same user, who always has the same name.
  2. Constant instance, variable property – a signpost that always points to the same user, but their name can change.
  3. Variable instance, constant property – a signpost that can point to different users, but their names never change.
  4. Variable instance, variable property – a signpost that can point to different users, and those users can also change their names.

Quizzes

  • Creating your own classes — 9/12
  • Class inheritance — 10/12
  • Copying objects — 10/12 Frakkin’ struct vs class
  • Deinitializers — 10/12
  • Mutability — 10/12

Checkpoint 7

Your challenge is this: make a class hierarchy for animals, starting with Animal at the top, then Dog and Cat as subclasses, then Corgi and Poodle as subclasses of Dog, and Persian and Lion as subclasses of Cat.

But there’s more:

  1. The Animal class should have a legs integer property that tracks how many legs the animal has.
  2. The Dog class should have a speak() method that prints a generic dog barking string, but each of the subclasses should print something slightly different.
  3. The Cat class should have a matching speak() method, again with each subclass printing something different.
  4. The Cat class should have an isTame Boolean property, provided using an initializer.

I haven’t been able to complete this challenge yet. I really don’t understand where parameters go. Here is my evidence:

Screencap of error message that says 'Failed to produce diagnostic for expression; please submit a bug report'

I’ll come back to this another day.

Footnotes

  1. Famous Last Words ↩︎