Using Self in Swift

Learn how to use Swift's self keyword when writing object-oriented classes, and how that use differs from most of the other object-oriented programming languages.

We'll cover the following...

S## The Swift self keyword

Programmers familiar with other object-oriented programming languages may be in the habit of prefixing references to properties and methods with self to indicate that the method or property belongs to the current class instance. The Swift programming language also provides the self property type for this purpose and it is, therefore, perfectly valid to write code that reads as follows:

class MyClass {
    var myNumber = 1
 
    func addTen() {
        self.myNumber += 10
    }
}

In this ...