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.

See Also: Hashable

Declaration

var hashValue: Int { get }

Instance Methods

func <(_:rhs:)

Returns a Boolean value indicating whether the value of the first argument is less than that of the second argument.

This function is the only requirement of the Comparable protocol. The remainder of the relational operator functions are implemented by the standard library for any type that conforms to Comparable.

Parameters: lhs: A value to compare. rhs: Another value to compare.

Declaration

func <(lhs: ObjectIdentifier, rhs: ObjectIdentifier) -> Bool
func ==(_:y:)

Returns a Boolean value indicating whether two values are equal.

Equality is the inverse of inequality. For any values a and b, a == b implies that a != b is false.

Parameters: lhs: A value to compare. rhs: Another value to compare.

Declaration

func ==(x: ObjectIdentifier, y: ObjectIdentifier) -> Bool