Skip to content

Instantly share code, notes, and snippets.

@alexanderedge
Last active December 14, 2015 01:28
Show Gist options
  • Save alexanderedge/5005981 to your computer and use it in GitHub Desktop.
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:`
#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