Hey what the heck is ‘===’ operator in swift language?

KD Knowledge Diet
3 min readApr 24, 2023

Swift is a powerful programming language used to develop a wide range of applications for iOS, macOS, watchOS, and tvOS platforms. In Swift, the === operator is a reference equality operator that checks whether two objects are the same instance or not. This operator is particularly useful when comparing instances of a class or a subclass.

The === operator can be used to check whether two objects reference the same memory location. If two objects have the same memory address, the === operator returns true. Otherwise, it returns false. Here's an example of how to use the === operator:

let object1 = Object()
let object2 = object1
let object3 = Object()

print(object1 === object2) // true
print(object1 === object3) // false

In the example above, we first create an instance of the Object class and assign it to the object1 constant. Then, we assign object1 to object2, which means both constants reference the same memory location. Finally, we create another instance of the Object class and assign it to the object3 constant. When we use the === operator to compare object1 and object2, it returns true because they reference the same memory location. However, when we compare object1 and object3, it returns false because they reference different memory locations.

The === operator can also be useful in unit testing. In a unit test, we may want to check whether an array contains a specific instance of a class. To do this, we can use the contains method along with the === operator to check whether any element in the array references the same memory location as the instance we are looking for. Here's an example of how to use the === operator in a unit test:

class Enemy {
var health: Int = 100
}

class Player {
var destroyedEnemies: [Enemy] = []
func attack(_ enemy: Enemy) {
enemy.health -= 50
if enemy.health <= 0 {
destroyedEnemies.append(enemy)
}
}
}

class PlayerTests: XCTestCase {
func testDestroyingEnemy() {
let player = Player()
let enemy = Enemy()
player.attack(enemy)
XCTAssertTrue(player.destroyedEnemies.contains { $0 === enemy })
}
}

In this example, we have a Player class that can attack and destroy Enemy instances. We want to test whether the Player instance successfully adds destroyed Enemy instances to its destroyedEnemies array. To do this, we first create an instance of Player and Enemy. Then, we call the attack method on the Player instance with the Enemy instance as a parameter. Finally, we use the contains method along with the === operator to assert that the destroyedEnemies array of the Player instance contains the same instance of the Enemy class that we passed as a parameter.

By using the === operator, we can ensure that the Enemy instance that was destroyed is the same instance that was passed to the attack method of the Player instance. This ensures that our unit test is testing the correct behavior of our code.

In conclusion, the === operator is a useful tool when comparing instances of a class or a subclass in Swift. By checking whether two objects reference the same memory location, we can ensure that our code is behaving correctly and our unit tests are testing the correct behavior.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!