美文网首页
class_respondsToSelector

class_respondsToSelector

作者: Winny_园球 | 来源:发表于2018-04-26 14:08 被阅读0次

    class_respondsToSelector 判断某个类是否有某个实例方法,有则返回YES,否则返回NO

    /**  
     * Returns a Boolean value that indicates whether instances of a class respond to a particular selector. 
     *  
     * @param cls The class you want to inspect. 
     * @param sel A selector. 
     *  
     * @return \c YES if instances of the class respond to the selector, otherwise \c NO. 
     *  
     * @note You should usually use \c NSObject's \c respondsToSelector: or \c instancesRespondToSelector:  
     *  methods instead of this function. 
     */  
    OBJC_EXPORT BOOL class_respondsToSelector(Class cls, SEL sel)   
         __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);  
    

    测试:

    - (void)viewDidLoad {  
        [super viewDidLoad];  
        // Do any additional setup after loading the view, typically from a nib.  
          
        //其中printDZL是Person类的实例方法,printDZ是Person类的类方法  
        Person * p1 = [[Person alloc] init];  
        BOOL isOrNot = class_respondsToSelector([p1 class], @selector(printDZL));  
        BOOL isOrNot2 = class_respondsToSelector([Person class], @selector(printDZ));  
        NSLog(@"%i--%i", isOrNot, isOrNot2);  
    }  
    

    输出结果

    1.  2015-11-04 13:53:33.019 02-runtime[3053:76771] 1--0
    

    相关文章

      网友评论

          本文标题:class_respondsToSelector

          本文链接:https://www.haomeiwen.com/subject/kcnplftx.html