Skip to content

Instantly share code, notes, and snippets.

@advantis
Created February 7, 2014 13:40
Show Gist options
  • Save advantis/8862784 to your computer and use it in GitHub Desktop.
Save advantis/8862784 to your computer and use it in GitHub Desktop.
Automagically generated object description
//
// Copyright © 2014 Yuri Kotov
//
#import <Foundation/Foundation.h>
@interface NSObject (ADVDescription)
- (NSString *) autoDescription;
@end
//
// Copyright © 2014 Yuri Kotov
//
#import "NSObject+ADVDescription.h"
#import <objc/runtime.h>
@implementation NSObject (ADVDescription)
- (NSString *) autoDescription
{
Class class = self.class;
NSMutableString *description =
[NSMutableString stringWithFormat:@"<%@: %p> {\n",
NSStringFromClass(class),
(__bridge void *) self];
do
{
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList(class, &count);
for (int i = 0; i < count; ++i)
{
@autoreleasepool
{
objc_property_t property = properties[i];
const char *name = property_getName(property);
NSString *key = [[NSString alloc] initWithBytesNoCopy:(void*)name
length:strlen(name)
encoding:NSASCIIStringEncoding
freeWhenDone:NO];
id value = [self valueForKey:key];
[description appendFormat:@"\t%@ = %@;\n", key, value];
}
}
free(properties);
}
while ([NSObject class] != (class = [class superclass]));
[description appendString:@"}"];
return description;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment