struct ObjectIdentifier

A unique identifier for a class instance or metatype.

In Swift, only class instances and metatypes have unique identities. There is no notion of identity for structs, enums, functions, or tuples.

Inheritance Comparable, CustomDebugStringConvertible, Equatable, Hashable View Protocol Hierarchy →
Import import Swift


init(_: Any.Type)

Creates an instance that uniquely identifies the given metatype.

Parameter: A metatype.


init(_ x: Any.Type)
init(_: AnyObject)

Creates an instance that uniquely identifies the given class instance.

The following example creates an example class A and compares instances of the class using their object identifiers and the identical-to operator (===):

class IntegerRef {
    let value: Int
    init(_ value: Int) {
        self.value = value

let x = IntegerRef(10)
let y = x

print(ObjectIdentifier(x) == ObjectIdentifier(y))
// Prints "true"
print(x === y)
// Prints "true"

let z = IntegerRef(10)
print(ObjectIdentifier(x) == ObjectIdentifier(z))
// Prints "false"
print(x === z)
// Prints "false"

x: An instance of a class.


init(_ x: AnyObject)

Instance Variables

var debugDescription: String

A textual representation of the identifier, suitable for debugging.


var debugDescription: String { get }
var hashValue: Int

The identifier's hash value.

The hash value is not guaranteed to be stable across different invocations of the same program. Do not persist the hash value across program runs.


var hashValue: Int { get }