Why is objc_duplicateClass marked as "Do not call this function yourself"

Documentation for objc_duplicateClass

easy read

Used to monitor the key values ​​of the fund. Don't call this function yourself.

without any clarification or explanation. I read the source but saw nothing in it, which gave me any indication as to why you shouldn't name it, other than a few theories; except that load

and initialize

are not called for classes created objc_duplicateClass

?

It looks like you could produce almost the same effect by calling objc_allocateClassPair

no extra bytes for the extra ivars and then registering it right away. So what's so special about objc_duplicateClass

? Or, to rephrase the question, if presumably I wanted to build a KVO or something very similar, on top of the objc runtime, would it be safe to use objc_duplicateClass

?

+3


source to share


2 answers


objc_duplicateClass

does pretty much what it says about the gesture: it creates a new class that is a copy of the class you give it. This includes copying the methods that the old class used, all of its metadata bits, and even its metaclass.

You cannot recreate this behavior with objc_allocateClassPair

, because it objc_allocateClassPair

registers a new metaclass for the class you are creating, among other things, as a class initialization - it is for creating a new class. objc_duplicateClass

creates a copy; the "new" class is not initialized further and does not get a new metaclass because the original did already.



As for why being objc_duplicateClass

documented the way it is: discourage its use. objc_duplicateClass

has some rare use cases and will almost never be what you want. If you find you need it, use it, but keep in mind that there are subtleties of implementation that you may not know about.

+1


source


Even I marked Q as opinion based, I would like to try to answer:

and. Such a statement usually indicates that a feature (or whatever) is deprecated and planned to be removed in the future.

Q. Indeed, there is no reason for this function. I disagree with your statement that registering a new class with zero extra bytes will do the job. because maybe there are additional bytes and you have to manually copy all descriptions and methods of the instance instance manually.



However, I have had to deal with situations like KVO in the past. I've never used objc_duplicateClass()

because t is much simpler and akin to a cleaner to create a subclass and then make the instance a member of that subclass. You can do isa-swizzling for this call object_setClass(id object, Class cls)

.

Think about the situation, it is obvious that this is yours, that another technology wants to do something like KVO. Duplicating a using class objc_duplicateClass()

does not participate in future dynamic changes to the original class. Maybe a method has been added to it. Duplication would not add this method either.

So it is objc_duplicateClass()

not useful and bad approach.

0


source







All Articles