Last active
December 14, 2015 01:28
-
-
Save alexanderedge/5005981 to your computer and use it in GitHub Desktop.
Adding this to any class conforming to NSObject protocol will print out the instance's properties in a friendly format when passed to `+ (NSString *)stringWithFormat:`
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
#import <objc/runtime.h> | |
- (NSString *)description{ | |
NSMutableString *objectDescription = [NSMutableString stringWithFormat:@"<%@: %p ",NSStringFromClass([self class]),self]; | |
unsigned int outCount, i; | |
objc_property_t *properties = class_copyPropertyList([self class], &outCount); | |
for (i = 0; i < outCount; i++) { | |
objc_property_t property = properties[i]; | |
id propertyValue = [self valueForKey:[NSString stringWithUTF8String:property_getName(property)]]; | |
[objectDescription appendFormat:@"%@:%@ ",[NSString stringWithUTF8String:property_getName(property)],[propertyValue description]]; | |
} | |
free(properties); | |
[objectDescription appendString:@">"]; | |
return objectDescription; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment