Almost all Swift applications will be object oriented. Chapter 1, Exploring Swift, and Chapter 2, Playing with Swift, both demonstrated functional and procedural Swift code. Classes such as Process
from the CoreFoundation
framework, and UIColor
and UIImage
from the UIKit
framework were used to demonstrate how classes can be used in applications. This section describes how to create classes, protocols, and enums in Swift.
A class is created in Swift using the class
keyword and braces are used to enclose the class body. The body can contain variables called properties as well as functions called methods, which are collectively referred to as members. Instance members are unique to each instance, while class members are shared between all instances of that class.
Classes are typically defined in a file named for the class; so a GitHubRepository
class can be defined in the GitHubRepository.swift
file. A new Swift file can be created by navigating...