Swift: Classes

Classes in Swift:

Let’s begin with Classes in Swift.

Swift Class:

A class is a programmatic representation of a real-life item in code. The keyword class is followed by the class name and curly braces that store the properties and methods of the class.

// Using data types:
class Employee {
  var name: String
  var age: Int
  var height: Double
  var experienced: Bool
}
 
 
// Using default property values:
class Employee {
  var name = ""
  var age = 0
  var height = 0.0
  var experienced = true
}

Instance of a Class in Swift:

Calling a defined class name with parentheses () and any necessary arguments creates a new instance of the class.

class Student {
  var name = ""
  var age = 0
}
 
var karl = Student()
 
// karl is now an instance of Student

Class Properties:

To access class properties, use the dot syntax, i.e. .property.

var lisa = Employee()
 
lisa.name = "Lisa Kudrow"
lisa.age = 22
lisa.height = 5.6
lisa.experienced = true

init() Method:

An init() method and corresponding initialized properties can use to initialize classes. The init() method uses the self keyword to refer to the actual instance of the class that assigns property values.

class Pet {
  var canFly = true 
  var color: String
 
  init(color: String) {
    self.color = color
  }
}
 
let parrot = Pet(color: "green")

Inheritance:

A class can inherit (or take on) the properties and methods of another class:

  • A subclass is a class that inherits from another class.
  • The superclass is the class from which the subclass inherits.
// Suppose we have a Employee class:
 
class Employee {
  var pay = 0.0 ;
 
  func addPay(value: Double) {
    pay += value ;
  }
 
  func deductFine(value: Double) {
    pay -= value ;
  }
} ;
 
 
// And we want a Manager class that inherits from Employee:
 
class Manager: Employee {
  var overtime = 0.0 ;
  var totalFines = 0 ;


} ;
 
// Here, the Manager class (subclass) automatically gains all of the characteristics of Employee class (superclass).

Overriding:

A subclass can provide its own custom implementation of a superclass-inherited property or function. Overriding is the term for this.

// Suppose we have that same Employee class:
 
class Employee {
  var pay = 0.0 ;
 
  func addPay(value: Double) {
    pay += value ;
  }
 
  func deductFine(value: Double) {
    pay -= value ;
  }
} ;
 
 
// And we want a Manager class that overrides a function of the parent class, for doing an operation a bit differently:
 
class Manager: Employee {
  var overtime = 0.0 ;
  var totalFines = 0 ;

  func addOvertime() {
    overtime = pay * 0.4 ;
    self.addPay(value: overtime) ;
  }

  override func deductFine(value: Double) {
    pay -= value ;
    totalFines += 1 ;
  }

} ;
 
// Here, the Manager class defines a .addOvertime() method, and overrides the .deductFine() function to keep track of the total fines .

Reference Types:

Classes are reference types, whereas structures are value types; structures are value types, but classes are reference types.

When reference types are assigned to a variable or constant, or supplied to a function, they are not copied, unlike value types. So, a reference to the same existing instance is used instead of a copy.

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: