Swift

Classes

Another complex data type

What are classes?

Along with structs, classes are another way Swift builds complex data types. However, they are a bit different. In structs, you have automatic initialization, but in classes, we have to write our own. Along with this, we can define new classes based on other classes (parent and child classes). There are also other terms. When we create new instances of a class, we call them an object. Also, when we copy objects, both point to the same stored data, so they both directly influence each other (Important to keep in mind!).

Let’s turn our Book struct into a Class.

class Book {
    var color: String
    var pages: Int
    init(color: String, pages: Int) {
        self.color = color
        self.pages = pages
    }
}

Our init function doesn’t have func because it’s a special method that Swift expects you to have. The parameter names are the same as the properties, so we use self. In the example, the color property of this class is set to the color parameter which was passed through the init function when an object is made from this class or 'blueprint'.

Inheritance

Inheritance allows us to build classes and let them have a parent-child relationship. Let’s add a function to our Book class.

class Book {
    var name: String
    var color: String
    var pages: Int
    init(name: String, color: String, pages: Int) {
        self.name = name
        self.color = color
        self.pages = pages
    }
    func description() {
        print("\(name) is \(color) with \(pages) pages.")
    }
}

// Now let's use this class to make an object. Here the class acts as a 'blueprint' for the object.
var bookOne = Book(name: "Diary of a Wimpy Kid", color: "Red", pages: "221")
bookOne.name
bookOne.pages
bookOne.description()

The description function contains information from the properties. Properties are variables and constants within types. This is our parent class, and we are going to make some child objects from it.

The way to do is below:

class NonFictionBook: Book {

}

Notice how we made a new class, but made it based on the original Book class as a 'blueprint'. In this class, we are going to override the description function, using override.

class NonFictionBook: Book {
    override func description() {
    print("This is a nonfiction book, all real!")
    }
}
// Using the class to create an object below
var historyBook = NonFictionBook(name:"World History", color: "Green", pages=1000)
historyBook.description()

If you replace historyBook’s class to Book instead of NonFictionBook, you will see the message change. Now, let’s create a pictureBook class. This one will have a new variable called price. To do this, we will need to initialize our parameters.

class pictureBook: Book {
    var price: Double
    init(name: String, age: Int, price: Double) {
        self.price = price
        super.init(name: name, age: age)
    }
    override func description() {
        print("This is a picture book with pictures!")
    }
}

We use super.init to pull in values from the parent class, but use a normal parameter initialization for the price variable.

chevron_up