Created
February 7, 2014 13:40
-
-
Save advantis/8862784 to your computer and use it in GitHub Desktop.
Automagically generated object description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright © 2014 Yuri Kotov | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSObject (ADVDescription) | |
- (NSString *) autoDescription; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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