Skip to content

Swift Classes

New Course Coming Soon:

Get Really Good at Git

This tutorial belongs to the Swift series

Classes are a bit similar to structures, but they have some key differences.

A class is defined using this syntax:

class Dog {

}

Inside a class you can define stored properties:

class Dog {
    var age = 0
}

A class definition defines a type. To create a new instance with this type, we use this syntax:

let roger = Dog()

Once you have an instance, you can access its properties using the dot syntax:

let roger = Dog()
roger.age

The same dot syntax is used to update a property value:

roger.age = 9

One big difference is that classes are reference types. Structures (and enumerations) are value types.

This means that assigning a class instance to another variable does not copy the instance. Both variables point to the same instance:

class Dog {
    var age = 0
}

let roger = Dog()
let syd = roger

roger.age = 9
//syd.age == 9

This also means we can define a reference to a class using let, and we can change its properties, as you saw in the example above.

We can create instances of classes, and we call them objects.

As with structs, classes can have properties, methods, and more.

Contrary to structs, we must define an initializer in order to initialize the values when we create an instance:

class Dog {
    var age : Int
    
    init(age: Int) {
        self.age = age
    }
}

let roger = Dog(age: 9)

You can only declare properties without initializing them if you have an initializer.

See the use of self. We need it because age is both an instance property and the init(age:) method parameter. self.age references the age instance property.

Classes can have instance methods: functions that belong to an instance of a class.

class Dog {
    var age = 8
    var name = "Roger"

    func bark() {
      print("\(name): wof!")
    }
}

And we also have type methods:

class Dog {
    var age = 8
    var name = "Roger"

    func bark() {
        print("\(name): wof!")
    }
    static func hello() {
        print("Hello I am the Dog struct")
    }
}

Invoked as Dog.hello()

One important thing classes allow is inheritance.

A class can inherit all the properties and methods from another class.

Say we have a class Animal. Every animal has an age:

class Animal {
    var age: Int
}

Not every animal has a name. Dogs have a name. So we create a Dog class extending from Animal:

class Dog: Animal {
    var name: String
}

Now we must add an initializer for both classes. In the Dog case, after we do the class-specific initialization, we can call the parent class initializer using super.init():

class Animal {
    var age: Int
    
    init(age: Int) {
        self.age = age
    }
}

class Dog: Animal {
    var name: String
    
    init(age: Int, name: String) {
        self.name = name
        super.init(age: age)
    }
}

var horse = Animal(age: 8)
var roger = Dog(age: 8, name: "Roger")

Animal is now a superclass, and Dog is a subclass.

There’s more to say about classes, but this is a good introduction.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my Swift Handbook

Here is how can I help you: