Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift

How to use type casting in Swift

Muhammad Saad Mehmoon

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Type casting is a method used to check the type of a variable or to treat it as a different superclass/subclass. Swift implements type casting using is and as operators.

Type checking

The keyword is can be used to check the type of a variable.

Code

var aVariable:Any = 4
if aVariable is Int {
print(aVariable, "is an integer")
} else {
print(aVariable, "is not an integer")
}
if aVariable is String {
print(aVariable, "is a string")
} else {
print(aVariable, "is not a string")
}
Code to check the type of a variable

Explanation

  • Line 1: We assign a value of 4 to a variable of type Any.
  • Lines 3–7: We check if aVariable is an integer and print the appropriate message.
  • Lines 9–13: We check if aVariable is a string and print the appropriate message.

Upcasting/downcasting

A variable of a class may refer to an instance of its subclass or superclass. The keyword as can be used to upcast or downcast a variable. Upcasting is treating that variable as a superclass instance, while downcasting is treating it as a subclass instance.

There are two cast operators used for this:

  • as?: This operator is used when we are not sure if the downcast will succeed. If the downcast succeeds, it will return an optional value of the type we're trying to downcast to. Otherwise, it will just return nil.
  • as!: This operator is used when we know that the downcast will always succeed. If it does not succeed, it will throw a runtime error.

Code

class Person {
var name: String
init(name: String) {
self.name = name
}
}
class Employee: Person {
var salary: Int
init(name: String, salary: Int) {
self.salary = salary
super.init(name:name)
}
}
class Student: Person {
var rollNumber: Int
init(name: String, rollNumber: Int) {
self.rollNumber = rollNumber
super.init(name:name)
}
}
let anEmployee = Employee(name: "Ali", salary: 400)
let aStudent = Student(name: "Educative", rollNumber: 500)
// Forcing upcast to person
let personOne = anEmployee as Person
let personTwo = aStudent as Person
// checking downcasting with as?
print(personOne as? Student ?? "Person One is not a student")
print(personTwo as? Student ?? "Person Two is not a student")
Code to demonstrate upcasting and downcasting

Explanation

  • Lines 1–22: We declare three classes, namely Person, Student, and Employee. The classes Student and Employee inherit from the Person class.
  • Lines 24–29: We initialize instances of the Employee and Student classes and then upcast them to the Person class using the as operator.
  • Lines 32–33: We use the keyword as? to check if personOne and personTwo are instances of the Student class. The hard-coded string is the default value returned by ?? in case it gets a Nil. Otherwise, it returns the downcasted optional value of the class, which we can see when we run the code.

RELATED TAGS

swift

CONTRIBUTOR

Muhammad Saad Mehmoon
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring