ios - How to avoid accidental invocation on UIKit's private classes? -
ios - How to avoid accidental invocation on UIKit's private classes? -
i have encountered weird problem. in current project working complex views , doing modifications on fly.
e.g. if view responds settextcolor setting new color using next code
if ([view respondstoselector:propertyselector]) {     // invoke method. }    accidentally calling method returned uibuttonlabel (which private hidden class in uikit), , executed above method. in above code uibuttonlabel responded settextcolor: application crashed in invocation. 
how avoid accidental access , invocation on uikit's private classes ?
yes can traverse reverse superclasses chain starting nsobject class, checking method not part of class_copymethodlist inspected class , it's metaclass (for class methods). while doing so, check inspected class not coming [nsbundle mainbundle] ([nsbundle bundleforclass:]) does, can terminate search - reached code. but, can imagine, caching it's really slow way of doing things.
you  improve of redesigning code:  seek using conformstoprotocol: instead of respondstoselector.  illustration declare protocol
@protocol xyzsettablecolor <nsobject>  @required - (void)setsomethingwithcolor:(uicolor *)textcolor;  @end    then, in class (classes) want declare conforms protocol (and implement required methods)
@interface xyzmycontrol : uicontrol <xyzsettablecolor>  @end    then later, when want create sure passed component qualified execute specified method:
- (void)applytextcolor:(uicolor *)color tocontrol:(id)control {     if ([control conformstoprotocol:@protocol(xyzsettablecolor)]) {         [(id<xyzsettablecolor>)control setsomethingwithcolor:color];     } }        ios uiview uikit private-methods 
 
Comments
Post a Comment