A class is used to programmatically represent a real-life object in code. Classes are defined by the keyword class
followed by the class name and curly braces that store the class’s properties and methods.
// Using data types:class Student {var name: Stringvar year: Intvar gpa: Doublevar honors: Bool}// Using default property values:class Student {var name = ""var year = 0var gpa = 0.0var honors = false}
Creating a new instance of a class is done by calling a defined class name with parentheses ()
and any necessary arguments.
class Person {var name = ""var age = 0}var sonny = Person()// sonny is now an instance of Person
Class properties are accessed using dot syntax, i.e. .property
.
var ferris = Student()ferris.name = "Ferris Bueller"ferris.year = 12ferris.gpa = 3.81ferris.honors = false
init()
MethodClasses can be initialized with an init()
method and corresponding initialized properties. In the init()
method, the self
keyword is used to reference the actual instance of the class assign property values.
class Fruit {var hasSeeds = truevar color: Stringinit(color: String) {self.color = color}}let apple = Fruit(color: "red")
A class can inherit, or take on, another class’s properties and methods:
// Suppose we have a BankAccount class:class BankAccount {var balance = 0.0func deposit(amount: Double) {balance += amount}func withdraw(amount: Double) {balance -= amount}}// And we want a new SavingsAccount class that inherits from BankAccount:class SavingsAccount: BankAccount {var interest = 0.0func addInterest() {let interest = balance * 0.005self.deposit(amount: interest)}}// Here, the new SavingsAccount class (subclass) automatically gains all of the characteristics of BankAccount class (superclass). In addition, the SavingsAccount class defines a .interest property and a .addInterest() method.
A subclass can provide its own custom implementation of a property or method that is inherited from a superclass. This is known as overriding.
// Suppose we have a BankAccount class:class BankAccount {var balance = 0.0func deposit(amount: Double) {balance += amount}func withdraw(amount: Double) {balance -= amount}}// Suppose we want a new SavingsAccount class and we want to override the .withdraw() method from its superclass BankAccount:class SavingsAccount: BankAccount {var interest = 0.0var numWithdraw = 0func addInterest() {let interest = balance * 0.01self.deposit(amount: interest)}override func withdraw(amount: Double) {balance -= amountnumWithdraw += 1}}
Classes are reference types, while structures are value types.
Unlike value types, reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used.