Это отличный вопрос!Поскольку я не нашел ничего подобного, я написал небольшую функцию, которая сделает это за вас.Он заменяет - (NSString *)description
из NSObject
, используя метод swizzling (да, я - это зло. MAHAHAHAHAHahahahaha) и печатает ивар в порядке их появления в классе (вы можете легко отредактировать его, чтобы отобразить в алфавитном порядке).
НЕ!забудьте позвонить 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);
}