ios - Inject custom class into subclass inheritance tree in Objective-C -
I think of myself as an experienced objective-programmer. I approve to be a survivor, and use the facilities of language completely. It involves using runtime, only-in-time changes are different in existing frameworks, like swizzling method and dynamic subclassification, the fact that this language is so versatile that it is equivalent to another object oriented equivalent .
But recently I have been thinking about an old facility, that I still think myself needs to use from time to time, but for some time now it disliked it Has been done. This is a replacement for the old class_setSuperClass .
I often find the UIKit classes subclasses in their order to expand them or just slightly alter their behavior. An example that I recently came to was a
UIScrollView subclass, which had created some conditions for
pangestarrequiser I did that by classifying
UIScrollView and then
apply gestureRecognizer: shouldBeRequiredToFailByGestureRecognizer:.
This is to be done in this way, because you do not have permission to change the pan gesture of a scroll view to the recogniser's representative (this one throws an exception exception), and you Can not create your own custom UIScrollView
panGestureRecognizer
Now all this worked great out. I changed all the uses of
UITableView and
UICollectionView which I have also used for the whole app, what to do with those people? As you know, both of these sections are inherited from
UIScrollView , but there is no my custom sub-class for
UIScrollView . That's why I ended up writing the same code several times for every class inherited from
UIScrollView (and it was used in the app) . Writing the same code multiple times is not programmer 1-2-3. But I had finished writing a custom subclass for
UIScrollView but instead of
UITableView and
UICollectionView
. With the
class_setSuperClass you were able to already have a pre-package class super class
UITableView like "sly" . You basically just call
class_setSuperClass ([UITableView class], [MyScrollView class]), and everything went fine (ok almost) okay. Now you injected your code between
UITableView and
UIScrollView . So every time you did
[[UITableView alloc] init] , there were attributes of
MyScrollView because it was inherited directly from it.
Now was excluded from
Class_setSuperClass iOS 2.0! Since we can swing and hacking some of the object's
isa pointer. Now it has also been deprecated.
So my question is really simple how will you approach this?
After "text" itemprop = "text">
You want to change the behavior of a class globally to play with the class instead of identifying , The direct approach just needs to change the method you have to replace. Runtime still allows it with
class_replaceMethod () . It seems that what you want is the easiest way to complete it.
Comments
Post a Comment