用于打印ivar值的通用Objective-C描述方法

时间:2011-06-16 17:52:25

标签: objective-c override categories nsobject

在Objective-C中,通常使用打印对象ID和实例变量名称/值的方法覆盖-description。我想制作一个通用的-description方法,通过内省来实现这一点,而不是为每个类手动执行此操作。我希望输出类似于:

<ClassName: 0x??????, ivar1: value1, ivar2: value2, ivar3: value3, ...>

按实例变量名排序也是很好的(因此它们总是以相同的顺序排列)。最后,如果这可以安全地覆盖NSObject功能的类别,那将是完美的(但我发现这并不简单,因为NSObject.m有关于在{中使用-[NSString stringWithFormat:]的警告{1}})。

2 个答案:

答案 0 :(得分:13)

这是一个很好的问题!因为我还没有找到这样的东西,所以我写了一个小函数来为你做这个。它取代- (NSString *)description NSObject使用方法调整(是的,我是这个邪恶的.MAHAHAHAHAHahahahaha)并按照它们也出现在课堂中的顺序打印ivar(您可以轻松编辑它以显示它们)字母顺序)。

不要!忘记致电NSObjectSwizzleDescription()

.h文件:

@interface NSObject (JSObjectAdditions)
@end


void NSObjectSwizzleDescription();

.m文件:

#import <objc/objc.h>
#import "JSObject.h"

@implementation NSObject (JSObjectAdditions)

- (NSString *)verboseDescription
{
    NSMutableString *description = [NSMutableString stringWithFormat:@"<%@: %p>", NSStringFromClass([self class]), self];

    uint32_t ivarCount;
    Ivar *ivars = class_copyIvarList([self class], &ivarCount);

    if(ivars)
    {
        [description appendString:@"\n{"];

        for(uint32_t i=0; i<ivarCount; i++)
        {
            Ivar ivar = ivars[i];
            const char *ivarType = ivar_getTypeEncoding(ivar);
            id ivarObject = object_getIvar(self, ivar);

            [description appendFormat:@"\n   %s: ", ivar_getName(ivar)];

            // Default signed data types
            if(strcmp(ivarType, "c") == 0)
            {
                char character = (char)ivarObject;
                [description appendFormat:@"'%c'", character];
            }
            else if(strcmp(ivarType, "i") == 0 || strcmp(ivarType, "l") == 0) // l is also 32 bit in the 64 bit runtime environment
            {
                int integer = (int)ivarObject;
                [description appendFormat:@"%i", integer];
            }
            else if(strcmp(ivarType, "s") == 0)
            {
                short shortVal = (short)ivarObject;
                [description appendFormat:@"%i", (int)shortVal];
            }
            else if(strcmp(ivarType, "q") == 0)
            {
                long long longVal = (long long)ivarObject;
                [description appendFormat:@"%l", longVal];
            }
            // Default unsigned data types
            else if(strcmp(ivarType, "C") == 0)
            {
                unsigned char chracter = (unsigned char)ivarObject;
                [description appendFormat:@"'%c'", chracter];
            }
            else if(strcmp(ivarType, "I") == 0 || strcmp(ivarType, "L") == 0)
            {
                unsigned int integer = (unsigned int)ivarObject;
                [description appendFormat:@"%u", integer];
            }
            else if(strcmp(ivarType, "S") == 0)
            {
                unsigned short shortVal = (unsigned short)ivarObject;
                [description appendFormat:@"%i", (int)shortVal];
            }
            else if(strcmp(ivarType, "Q") == 0)
            {
                unsigned long long longVal = (unsigned long long)ivarObject;
                [description appendFormat:@"%ll", longVal];
            }
            // Floats'n'stuff
            else if(strcmp(ivarType, "f") == 0)
            {
                float floatVal;
                memcpy(&floatVal, &ivarObject, sizeof(float));

                [description appendFormat:@"%f", floatVal];
            }
            else if(strcmp(ivarType, "d") == 0)
            {
                double doubleVal;
                memcpy(&doubleVal, &ivarObject, sizeof(double));

                [description appendFormat:@"%f", doubleVal];
            }
            // Boolean and pointer
            else if(strcmp(ivarType, "B") == 0)
            {
                BOOL booleanVal = (BOOL)ivarObject;
                [description appendFormat:@"%@", (booleanVal ? @"YES" : @"NO")];
            }
            else if(strcmp(ivarType, "v") == 0)
            {
                void *pointer = (void *)ivarObject;
                [description appendFormat:@"%p", pointer];
            }
            else if(strcmp(ivarType, "*") == 0 || strcmp(ivarType, ":") == 0) // SEL is just a typecast for a cstring
            {
                char *cstring = (char *)ivarObject;
                [description appendFormat:@"\"%s\"", cstring];
            }
            else if(strncmp(ivarType, "@", 1) == 0)
            {
                [description appendFormat:@"%@", ivarObject];
            }
            else if(strcmp(ivarType, "#") == 0)
            {
                Class objcClass = (Class)ivarObject;
                [description appendFormat:@"%s", class_getName(objcClass)];
            }
            else
                [description appendString:@"???"];
        }

        [description appendString:@"\n}"];
        free(ivars);
    }

    return description; 
}

@end


void NSObjectSwizzleDescription()
{
    Method origMethod = class_getInstanceMethod([NSObject class], @selector(description));
    Method newMethod = class_getInstanceMethod([NSObject class], @selector(verboseDescription));

    method_exchangeImplementations(origMethod, newMethod);
}

答案 1 :(得分:1)

我不知道这样做的任何代码,但如果存在,我会对调试目的感兴趣!

我很惊讶我没有找到类似这样的东西,classic techniques只是有效,但是我认为可以使用某种inspect方法确实很好使用反射,但也许我错了。

我建议您查看a this interesting post,这与您要查找的内容有关,并且可以作为实施它的基础。

另一件有趣的事情也是NSLogger,这是一个很好的调试工具。