如何在Objective-C中检查类? [英] How do I inspect a Class in Objective-C?

查看:77
本文介绍了如何在Objective-C中检查类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新我通过实现

Update I fixed up the code to eliminate duplication of overridden methods and track originator of property or method by implementing Mark's suggestion. Haven't tackled property types yet (will probably start with property_getAttributes() when I do). Also stripped out vestigial underscores.

基本上,我需要一种方法来提醒自己,给定对象可以使用哪些方法和属性,而不必一直跳到继承树上.

Basically I need a way to remind myself what methods and properties are available on a given object without hopping all the way down the inheritance tree.

我已经完成了以下功能,但还有一点不足:

I've cooked up the following function but it leaves a bit to be desired:

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

以及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)

推荐答案

好像您需要的是NSMutableDictionary,键入选择器名称.将每个选择器名称作为键添加到字典中,并将类名称作为值.您可以在添加选择器之前搜索字典,以消除重复.

Seems like what you need is an NSMutableDictionary, keyed on the selector names. Add each selector name to the dictionary as a key, with the class name as the value. You can search the dictionary before adding a selector, to eliminate duplicates.

这篇关于如何在Objective-C中检查类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆