Swift classes: Basics

Description: Classes are very similar to structures, but there are important differences to know when deciding which one to use.

What makes them different is that classes can inherit from other classes, have deinitializers and allow for multiple references to a class instance. Structures can’t do these things.

Declaring a class is exactly as with a structure, but you start with the keyword class.

Class declaration

Properties: Classes, as structs can have stored properties or computed properties. Stored properties can have observers and computed properties can have setters and getters (See Swift Computed Properties).

If you give all your properties default values, it seems that you do not need to use an initializer. In this case we are doing exactly this. The initializer is coming soon, and we will remove the default values then.

Class properties

Methods: Are functions associated with a class, same as in structs. However, different from a struct, if you want to update a property value within the class, you don’t need to use the keyword mutating for a method.

Class methods

Initializers: Classes do not have default initializers (structs do have them), meaning that you need to create your own initializer so that all the properties have values.

As you can see below, the initializer will assign values to the class’s properties, even if there are default values assigned.

Class initialization

Inheritance defines a relationship where a subclass can be defined in terms of a superclass. Inheritances is the main difference between classes and structures.

  • The subclass inherits all the features of the super class (properties, methods, subscripts), and has access to them through dot syntax.
  • As you can see below, I created the subclass InheritanceExample which inherits all the features of BudgetTest.
  • Without setting any features in the subclass, I can create an instance of InheritanceExample with the initializer of the superclass,
  • I can get access to the superclass features through dot syntax.
Accessing superclass features through the subclass, through dot syntax

A subclass can also put in place it’s own features

  • In the code below, printDescription and ownFeature are specific to this class.
  • You can also notice the use of super.init, which calls the initializer of the superclass. The process of initializing a subclass goes like this: you initialize the properties of the subclass first, after which you call the initializer of the superclass to set values to its own properties.
Calling the initializer of a superclass by using the super keyword

Overriding methods, properties and subscripts: we use the keyword override to indicate that the subclass will change a property, method or subscript, depending on what you want to change.

Override asks the compiler to verify that the superclass has the matching declaration that can be overridden, throwing an error in case there is no match.

Overriding a superclass method

If you have encountered the keyword super in some examples, know that it is used to access a superclass’s features.

See an example below:

Using the super keyword to access superclass features

In the code above I override a method and am calling the superclass implementation of that method by using the super keyword. This allows me to extend that superclass’s method, as I can add new features to it.

Preventing override? use the keyword final before the variable, method

Keyword final in front of the method to prevent override
Show your support

Clapping shows how much you appreciated Cosmin Mircea’s story.