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 !== (not identical to) operator is a binary comparison operator used to determine if two reference type variables point to different instances in memory. It evaluates to true if the operands reference distinct memory addresses, regardless of whether their underlying stored properties hold equivalent values.

Syntax

expression1 !== expression2
  • expression1, expression2: Operands must be of a reference type (i.e., instances of a class or types constrained to AnyObject).
  • Return Type: Bool.

Technical Mechanics

In Swift, memory management and instance allocation differentiate between value types (struct, enum) and reference types (class). The !== operator operates strictly at the pointer level for reference types. When the compiler evaluates A !== B, it compares the memory addresses of the heap allocations that A and B point to. It does not inspect the state or properties of the objects themselves.
class StateNode {
    var identifier: Int
    init(identifier: Int) { self.identifier = identifier }
}

let nodeX = StateNode(identifier: 256)
let nodeY = StateNode(identifier: 256)
let nodeZ = nodeX

// Evaluates to true: nodeX and nodeY are distinct heap allocations
let distinctMemory = (nodeX !== nodeY) 

// Evaluates to false: nodeX and nodeZ point to the exact same heap allocation
let sharedMemory = (nodeX !== nodeZ) 

Constraints and Rules

  1. Reference Types Only: Attempting to use !== on value types (like Int, String, or custom struct types) results in a compile-time error, as value types do not have stable, shared memory identities in the same way reference types do.
  2. Protocol Independence: The !== operator does not rely on or invoke the Equatable protocol. It is a low-level pointer comparison built into the Swift standard library.

Distinction from !=

It is critical to distinguish !== (identity inequality) from != (value inequality):
  • != (Not Equal To): Compares the semantic value or state of two instances. It requires the type to conform to the Equatable protocol and invokes the == function under the hood, negating the result.
  • !== (Not Identical To): Compares the memory address of two instances. It ignores semantic value entirely. Two objects can be equal in value (A == B is true) but not identical in memory (A !== B is true).
Master Swift with Deep Grasping Methodology!Learn More