ObjectIdentifier

struct ObjectIdentifier

A unique identifier for a class instance or metatype.

Inheritance Comparable, CustomDebugStringConvertible, Equatable, Hashable

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

Initializers

init init(_:) Required

Creates an instance that uniquely identifies the given class instance.

The following example creates an example class IntegerRef 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"
  • Parameter x: An instance of a class.

Declaration

@inlinable public init(_ x: AnyObject)
init init(_:) Required

Creates an instance that uniquely identifies the given metatype.

Declaration

@inlinable public init(_ x: Any.Type)

Instance Variables

var debugDescription Required

A textual representation of the identifier, suitable for debugging.

Declaration

var debugDescription: String

Instance Methods

func hash(into hasher: inout Hasher) Required

Hashes the essential components of this value by feeding them into the given hasher.

  • Parameter hasher: The hasher to use when combining the components of this instance.

Declaration

@inlinable public func hash(into hasher: inout Hasher)

Type Methods

func !=(lhs: Self, rhs: Self) -> Bool Required

Declaration

public static func !=(lhs: Self, rhs: Self) -> Bool
func ...(maximum: Self) -> PartialRangeThrough<Self> Required

Returns a partial range up to, and including, its upper bound.

Use the prefix closed range operator (prefix ...) to create a partial range of any type that conforms to the Comparable protocol. This example creates a PartialRangeThrough<Double> instance that includes any value less than or equal to 5.0.

let throughFive = ...5.0

throughFive.contains(4.0)     // true
throughFive.contains(5.0)     // true
throughFive.contains(6.0)     // false

You can use this type of partial range of a collection's indices to represent the range from the start of the collection up to, and including, the partial range's upper bound.

let numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[...3])
// Prints "[10, 20, 30, 40]"
  • Parameter maximum: The upper bound for the range.

Declaration

prefix public static func ...(maximum: Self) -> PartialRangeThrough<Self>
func ...(minimum: Self) -> PartialRangeFrom<Self> Required

Returns a partial range extending upward from a lower bound.

Use the postfix range operator (postfix ...) to create a partial range of any type that conforms to the Comparable protocol. This example creates a PartialRangeFrom<Double> instance that includes any value greater than or equal to 5.0.

let atLeastFive = 5.0...

atLeastFive.contains(4.0)     // false
atLeastFive.contains(5.0)     // true
atLeastFive.contains(6.0)     // true

You can use this type of partial range of a collection's indices to represent the range from the partial range's lower bound up to the end of the collection.

let numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[3...])
// Prints "[40, 50, 60, 70]"
  • Parameter minimum: The lower bound for the range.

Declaration

postfix public static func ...(minimum: Self) -> PartialRangeFrom<Self>
func ...(minimum: Self, maximum: Self) -> ClosedRange<Self> Required

Returns a closed range that contains both of its bounds.

Use the closed range operator (...) to create a closed range of any type that conforms to the Comparable protocol. This example creates a ClosedRange<Character> from "a" up to, and including, "z".

let lowercase = "a"..."z"
print(lowercase.contains("z"))
// Prints "true"

Declaration

public static func ...(minimum: Self, maximum: Self) -> ClosedRange<Self>
func ..<(maximum: Self) -> PartialRangeUpTo<Self> Required

Returns a partial range up to, but not including, its upper bound.

Use the prefix half-open range operator (prefix ..<) to create a partial range of any type that conforms to the Comparable protocol. This example creates a PartialRangeUpTo<Double> instance that includes any value less than 5.0.

let upToFive = ..<5.0

upToFive.contains(3.14)       // true
upToFive.contains(6.28)       // false
upToFive.contains(5.0)        // false

You can use this type of partial range of a collection's indices to represent the range from the start of the collection up to, but not including, the partial range's upper bound.

let numbers = [10, 20, 30, 40, 50, 60, 70]
print(numbers[..<3])
// Prints "[10, 20, 30]"
  • Parameter maximum: The upper bound for the range.

Declaration

prefix public static func ..<(maximum: Self) -> PartialRangeUpTo<Self>
func ..<(minimum: Self, maximum: Self) -> Range<Self> Required

Returns a half-open range that contains its lower bound but not its upper bound.

Use the half-open range operator (..<) to create a range of any type that conforms to the Comparable protocol. This example creates a Range<Double> from zero up to, but not including, 5.0.

let lessThanFive = 0.0..<5.0
print(lessThanFive.contains(3.14))  // Prints "true"
print(lessThanFive.contains(5.0))   // Prints "false"

Declaration

public static func ..<(minimum: Self, maximum: Self) -> Range<Self>
func <(lhs: ObjectIdentifier, rhs: ObjectIdentifier) -> Bool Required

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.

Declaration

@inlinable public static func <(lhs: ObjectIdentifier, rhs: ObjectIdentifier) -> Bool
func <=(lhs: Self, rhs: Self) -> Bool Required

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

This is the default implementation of the less-than-or-equal-to operator (<=) for any type that conforms to Comparable.

Declaration

@inlinable public static func <=(lhs: Self, rhs: Self) -> Bool
func ==(x: ObjectIdentifier, y: ObjectIdentifier) -> Bool Required

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.

Declaration

@inlinable public static func ==(x: ObjectIdentifier, y: ObjectIdentifier) -> Bool
func >(lhs: Self, rhs: Self) -> Bool Required

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

This is the default implementation of the greater-than operator (>) for any type that conforms to Comparable.

Declaration

@inlinable public static func >(lhs: Self, rhs: Self) -> Bool
func >=(lhs: Self, rhs: Self) -> Bool Required

Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.

This is the default implementation of the greater-than-or-equal-to operator (>=) for any type that conforms to Comparable.

Declaration

@inlinable public static func >=(lhs: Self, rhs: Self) -> Bool