Skip to content

Swift Classes

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.


→ Get my Swift Handbook

→ I wrote 17 books to help you become a better developer:

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook
...download them all now!

Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list