如何检查Objective-C中的类?

Sha*_*man 6 oop reflection objective-c

更新我修复了代码以消除重写方法的重复,并通过实施Mark的建议来跟踪属性或方法的创建者.还没有处理过属性类型(可能会property_getAttributes()在我做的时候开始).也删除了退化的下划线.


基本上我需要一种方法来提醒自己在给定对象上可用的方法和属性,而不必一直向下遍历继承树.

我已经完成了以下功能,但它有点不尽如人意:

#import <objc/runtime.h>

NSInteger inspectClass_alphabeticSort(id string1, id string2, void *reverse)
{
    if ((NSInteger *)reverse == NO)
    {
        return [string2 localizedCaseInsensitiveCompare:string1];
    }
    return [string1 localizedCaseInsensitiveCompare:string2];
}

void inspectClassStopAt(Class inspectedClass, Class stopClass)
{
    Class originalClass             = inspectedClass;
    NSString *originalClassString   = [NSString stringWithFormat:@"%@", originalClass];
    NSString *inheritancePath       = [NSString stringWithFormat:@"%@", originalClass];

    Method *methods;
    objc_property_t *properties;
    unsigned int i;
    unsigned int methodCount;
    unsigned int propertyCount;
    int reverseSort = NO;

    NSArray *sorted;
    NSArray *methodsAndPropertiesKeys;
    NSMutableDictionary * methodsAndProperties = [NSMutableDictionary dictionaryWithCapacity:10];

    NSString *inspectedClassString;
    NSString *methodOrPropertyName;
    while (inspectedClass != stopClass)
    {
        inspectedClassString = [NSString stringWithFormat:@"%@", inspectedClass];
        if (inspectedClass != originalClass)
        {
            inheritancePath = [inheritancePath stringByAppendingFormat:@" : %@", inspectedClass];
        }

        methods     = class_copyMethodList(inspectedClass, &methodCount);
        properties  = class_copyPropertyList(inspectedClass, &propertyCount);

        for (i=0; i<methodCount; i++)
        {
            methodOrPropertyName = [NSString stringWithFormat:@"-%s", sel_getName(method_getName(methods[i]))];

            if (![methodsAndProperties objectForKey:methodOrPropertyName])
            {
                [methodsAndProperties setObject:inspectedClassString forKey:methodOrPropertyName];
            }
        }

        for (i=0; i<propertyCount; i++)
        {
            methodOrPropertyName = [NSString stringWithFormat:@" %s", property_getName(properties[i])];

            if (![methodsAndProperties objectForKey:methodOrPropertyName])
            {
                [methodsAndProperties setObject:inspectedClassString forKey:methodOrPropertyName];
            }
        }

        inspectedClass = [inspectedClass superclass];
    }
    free(methods);
    free(properties);

    methodsAndPropertiesKeys = [methodsAndProperties allKeys];
    sorted = [methodsAndPropertiesKeys sortedArrayUsingFunction:inspectClass_alphabeticSort context:&reverseSort];

    NSLog(@"%@", inheritancePath);
    for (NSString *key in sorted)
    {
        if (![[methodsAndProperties objectForKey:key] isEqualToString:originalClassString])
        {
            NSLog(@"\t%@ (%@)", key, [methodsAndProperties objectForKey:key]);
        }
        else
        {
            NSLog(@"\t%@", key);
        }
    }
}

void inspectClass(Class inspectedClass)
{
    inspectClassStopAt(inspectedClass, [NSObject class]);
}
Run Code Online (Sandbox Code Playgroud)

以及一些样本输出inspectClass([TextMap class]);:

TextMap : Surface
     position (Surface)
     size (Surface)
    -addChild: (Surface)
    -dealloc
    -init (Surface)
    -initWithFile:
    -position (Surface)
    -render
    -setPosition: (Surface)
    -setSize: (Surface)
    -setText:
    -size (Surface)
    -update (Surface)
Run Code Online (Sandbox Code Playgroud)

Mar*_*sey 1

看来您需要的是一个 NSMutableDictionary,以选择器名称为键。将每个选择器名称作为键添加到字典中,以类名称作为值。您可以在添加选择器之前搜索字典,以消除重复项。