【问题标题】:PyObjC: how to delete existing Objective-C classPyObjC:如何删除现有的 Objective-C 类
【发布时间】:2011-09-09 12:45:58
【问题描述】:

我之前创建了一个 ObjC 类。我怎样才能再次删除它?因为在以后的某个时候,我想用另一个版本重新创建它。

现在,如果我只是重新声明它,我会收到异常 X is overriding existing Objective-C class

【问题讨论】:

    标签: objective-c pyobjc objective-c-runtime


    【解决方案1】:

    我不知道如何在 PyObjC 中执行此操作,但用于执行此操作的 Objective-C 运行时函数应该是 objc_disposeClassPair()。搜索了一下,发现在 PyObjC 中使用这个 Objective-C 运行时特性可能不起作用:

    2008 年 1 月的 PyObjC SVN 提交消息为:Initial attempt of using objc_disposeClassPair. Disabled because this causes an unexpected crash.http://blog.gmane.org/gmane.comp.python.pyobjc.cvs/month=20080101

    有问题的代码仍在 PyObjC 源代码的当前 class-builder.m 中,第 164 行,并带有一个有趣的注释前缀:

        /*
         * Call this when the python half of the class could not be created. 
         *
         * Due to technical restrictions it is not allowed to unbuild a class that
         * is already registered with the Objective-C runtime.
         */
        int 
        PyObjCClass_UnbuildClass(Class objc_class __attribute__((__unused__)))
        {
            PyObjC_Assert(objc_class != nil, -1);
            PyObjC_Assert(objc_lookUpClass(class_getName(objc_class)) == nil, -1);
    
            //objc_disposeClassPair(objc_class);
            return 0;
        }
    

    话虽如此,我从未在 Objective-C 本身中使用过它,而且我对 PyObjC 了解不多。希望这会有所帮助。

    【讨论】:

    • 我尝试通过 ctypes 调用objc_disposeClassPair,但我也立即崩溃了。我想知道为什么...
    • 啊,我成功了。我可能只是设置了 ctypes 函数的错误参数类型。 This example 工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多