Search code examples
objective-cpyobjcobjective-c-runtime

PyObjC: how to delete existing Objective-C class


I created a ObjC class earlier. How can I delete it again? Because at some later point, I want to recreate it by another version.

Right now, if I just redeclare it, I get the exception X is overriding existing Objective-C class.


Solution

  • I don't know how to do this in PyObjC, but the Objective-C runtime function for doing this should be objc_disposeClassPair(). Searching around a bit yielded an indication that using this Objective-C runtime feature might not work in PyObjC:

    A PyObjC SVN commit message from January 2008 reads: Initial attempt of using objc_disposeClassPair. Disabled because this causes an unexpected crash. http://blog.gmane.org/gmane.comp.python.pyobjc.cvs/month=20080101

    The code in question is still in the current class-builder.m of PyObjC's source in line 164ff and is prefixed with an interesting comment:

        /*
         * 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;
        }
    

    That being said, I never used this in Objective-C itself and I don't know much about PyObjC. Hope this helps.