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

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

#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;
}

  

原文地址:https://www.cnblogs.com/hbf369/p/4146381.html