def __new__(cls, ptr):
# If there is already an instance that wraps this pointer, return the same object...
# This makes it a little easier to put auxiliary data into the instance (e.g. to use in an ObjC callback)
# Note however that a new instance may be created for the same underlying ObjC object if the last instance gets garbage-collected.
if isinstance(ptr, ui.View):
ptr = ptr._objc_ptr
if isinstance(ptr, ObjCInstance):
return ptr
if isinstance(ptr, c_void_p):
ptr = ptr.value
cached_instance = _cached_instances.get(ptr)
if cached_instance:
return cached_instance
objc_instance = super(ObjCInstance, cls).__new__(cls)
_cached_instances[ptr] = objc_instance
if isinstance(ptr, ui.View):
ptr = ptr._objc_ptr
objc_instance.ptr = ptr
objc_instance._as_parameter_ = ptr
objc_instance._cached_methods = weakref.WeakValueDictionary()
objc_instance.weakrefs = weakref.WeakValueDictionary()
if ptr:
# Retain the ObjC object, so it doesn't get freed while a pointer to it exists:
objc_instance.retain(restype=c_void_p, argtypes=[])
return objc_instance
评论列表
文章目录