Object-Oriented Swift

Object-Oriented Programming (OOP) is based around the concept of Objects, where an Object is made up of Attributes, or Variables, that can contain data, and Methods, that describe actions of an Object. Attributes and Methods are collectively known as members of a class. A person can be used as a real-world example of an Object. A person has Attributes such as height, weight, eye colour, hair colour and so on, as well as Methods or actions such as, walk, talk and eat.

A Class is a blue print, or template, of an Object that describes its Attributes and Methods. From this template one or more Objects can be created, which is known as instantiating an Object. A Class can be compared to an architects drawing of a building, which describes how it needs to be built. From this drawing, one or more buildings can be made to the specification of the drawing.

In Swift, the basic structure of a Class is as follows.

// Class definition.
class ClassName
{
    
    // Attributes and methods go here.
    
}

Notice the capitalisation of the first letter of each word in the Class name. This is known as Pascal Case and is the standard method for naming Classes in Swift.

Below is an example of a Class called ‘Greeting’, along with how it can be instantiated, using the name ‘greeting’.

// Class definition.
class Greeting
{
    
    // Attributes and methods go here.
    
}

// Class instance.
var greeting = Greeting()

Adding Attributes

Attributes are added to Classes in Swift in the form of Variables.

// Class definition.
class Greeting
{
    
    // Attributes.
    var message = "Hello World!”
    
}

// Class instance.
var greeting = Greeting()

// Display the class attribute 'message'.
print(greeting.message)

Here an Attribute, ‘message’, has been added to the ‘Greeting’ Class. After the Object has been instantiated, the message is displayed.

Setting Attributes

Setting an Attribute of an Object is done in a similar way to accessing it. In this example a ‘name’ Attribute has been added to the ‘Greeting’ class. This is set to ‘Fred’ and concatenated with the rest of the message before being displayed.

// Class definition.
class Greeting
{
    
    // Attributes.
    var message = "Hello"
    var name = ""
    
}

// Class instance.
var greeting = Greeting()

// Set attribute.
greeting.name = "Fred"

// Format the greeting and display it.
print("\(greeting.message) \(greeting.name)!")

Adding Methods

Functions in a Class in Swift are known as Methods and carry out the actions of an Object. In the previous example the message is formed by concatenating two Attributes together, separated by a space, with an exclamation mark on the end. This formatting of the message could be carried out in a Method. The advantage of doing this is that it could be reused any number of times and if the format of the message needs changing it only needs to be altered in one place. In the ‘return’ statement, ‘self’ is used to reference the current object. The difference between referencing an Attribute and a Method is that with a Method the name is followed by ‘()’, as with ‘displayGreeting()’ below.

// Class definition.
class Greeting
{
    
    // Attributes.
    var message = "Hello"
    var name = ""
    
    // Method.
    func displayGreeting() -> String
    {
        return "\(self.message) \(self.name)!"
    }
    
}

// Class instance.
var greeting = Greeting()

// Set attribute.
greeting.name = "Fred"

// Display the greeting using the method.
print(greeting.displayGreeting())

Instead of setting attributes after the class has been instantiated, it could be done at the same time, by passing the values as parameters and adding an 'init' to the class.

// Class definition.
class Greeting
{
    
    // Attributes.
    var message: String
    var name: String
    
    // Initialiser.
    init (message: String, name: String)
    {
        self.message = message
        self.name = name
    }
    
    // Method.
    func displayGreeting() -> String
    {
        return "\(self.message) \(self.name)!"
    }
    
}

// Class instance.
var greeting = Greeting(message: "Hello", name: "Fred")

// Display the greeting using the method.
print(greeting.displayGreeting())

Within the initialiser, the 'self' keyword is used to reference the current instance of the class.