No, this is not possible.
ObjectIdentifier
is declared in the open sourced Swift Standard Library, so we can look at a snippet of it's implementation from GitHub (I have stripped out non-essential code and comments):
@frozen
public struct ObjectIdentifier {
internal let _value: Builtin.RawPointer
public init(_ x: AnyObject) {
self._value = Builtin.bridgeToRawPointer(x)
}
public init(_ x: Any.Type) {
self._value = unsafeBitCast(x, to: Builtin.RawPointer.self)
}
}
We see that the implementation's stored _value
is marked internal
, so you will not be able to access it from code defined in other modules at all. In addition, only a pointer to the object is stored, meaning there is no 'direct' access to the object anyway.
Your best option is to just keep around the object, and create an ObjectIdentifier
as and when you need it. Alternatively, you can re-implement ObjectIdentifier
in your own module to be able to access the underlying object/pointer.