ObjectIdentifier protocol : object identity 비교하기
31 May 2019 |
ObjectIdentifier
A unique identifier for a class instance or metatype.
객체의 Identity 가 같은지/다른지 판단해주는 struct
struct ObjectIdentifier
-
사용되는 곳
Example.
Equatable
protocolprotocol 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 instance 와 meta type 만 unique identity를 가진다
-
struct, enum, function, tuple 에는 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