Daheen Lee all white cheat sheet

ObjectIdentifier protocol : object identity 비교하기

ObjectIdentifier

A unique identifier for a class instance or metatype.

객체의 Identity 가 같은지/다른지 판단해주는 struct

struct ObjectIdentifier

 

  • 사용되는 곳

    Example. Equatable protocol

    protocol Equatable {
      @inlinable // trivial-implementation
      public func === (lhs: AnyObject?, rhs: AnyObject?) -> Bool {
        switch (lhs, rhs) {
        case let (l?, r?):
          return ObjectIdentifier(l) == ObjectIdentifier(r)
        case (nil, nil):
          return true
        default:
          return false
        }
      }
    }
    

     

  • Swift 에서는 class instancemeta type 만 unique identity를 가진다

  • struct, enum, function, tuple 에는 identity 란 개념이 없다

  • :mag: eqaulity vs. Identity

 

  • Identity Operator

    func === (lhs: AnyObject?, rhs: AnyObject?) -> Bool
    

    Returns a Boolean value indicating whether two references point to the same object instance.

    reference 가 같은 instance 를 가리키는지 판단

 


Reference