ObjectIdentifier

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

Initializers

init(_: Any.Type)

Creates an instance that uniquely identifies the given metatype.

Parameter: A metatype.

Declaration

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.

Declaration

init(_ x: AnyObject)

Instance Variables

var debugDescription: String

A textual representation of the identifier, suitable for debugging.

Declaration

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.

Declaration

var hashValue: Int { get }