判断一个对象是否实现了某方法,而非继承而来

 

 

判断一个对象是否实现了某方法,而非继承而来。

 

#import <objc/runtime.h>

- (BOOL)realRespondsToSelector:(SEL)selector
{
    BOOL result = NO;
    u_int count;
    Method *methods= class_copyMethodList([self class], &count);
    for (int i = 0; i < count ; i++)
    {
        SEL name = method_getName(methods[i]);
        
        if (name == selector)
        {
            result = YES;
            break;
        }
    }
    
    if (methods != NULL)
    {
        free(methods);
        methods = NULL;
    }
    
    return result;
}

  

posted @ 2014-12-05 12:19  翛尧  阅读(281)  评论(0编辑  收藏  举报