Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The === operator, known as the identity operator, evaluates whether two constants or variables point to the exact same object instance in memory. It returns a Boolean value: true if both operands reference the same memory address, and false otherwise.

Technical Characteristics

  • Applicable Types: The === operator applies to instances of classes, actors, and class-bound (or AnyObject) protocols. It cannot be used with value types (such as struct, enum, Int, or String) because value types are copied upon assignment and do not share memory addresses. Additionally, while functions and closures are reference types in Swift, the === operator cannot be used to compare them.
  • Pointer Equality: Under the hood, === performs a pointer comparison. It checks the memory address of the references rather than evaluating the data stored within the instances.
  • No Protocol Conformance Required: Unlike the equality operator (==), which requires types to conform to the Equatable protocol, === is available by default for supported object instances without any explicit protocol adoption.

Syntax Visualization

class ReferenceType {
    var value: Int
    init(value: Int) { self.value = value }
}

let instanceA = ReferenceType(value: 100)
let instanceB = instanceA                     // instanceB points to the same memory address as instanceA
let instanceC = ReferenceType(value: 100)     // instanceC is a new instance with identical data

// Identity Comparison
let isSameInstance = (instanceA === instanceB)      // Evaluates to true
let isDifferentInstance = (instanceA === instanceC) // Evaluates to false

Distinction from the Equality Operator (==)

It is critical to distinguish between identity (===) and equality (==):
  • === answers: Are these two references pointing to the same allocation in memory?
  • == answers: Do these two instances hold the same observable data/state? (Requires custom implementation via Equatable).

The Inverse Operator (!==)

Swift also provides the !== operator (the “not identical to” operator). It is the logical negation of === and returns true if two references point to different instances in memory.
let instancesDiffer = (instanceA !== instanceC) // Evaluates to true
Master Swift with Deep Grasping Methodology!Learn More