objcnew.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:pythonista-scripts 作者: khilnani 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号