Skip to content

Instantly share code, notes, and snippets.

@calebd
Last active December 10, 2015 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebd/4392732 to your computer and use it in GitHub Desktop.
Save calebd/4392732 to your computer and use it in GitHub Desktop.
A better NSObject description.
#import <objc/runtime.h>
- (NSString *)description {
unsigned int count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:count];
for (unsigned int i = 0; i < count; i++) {
NSString *name = [NSString stringWithUTF8String:property_getName(properties[i])];
id value = [self valueForKey:name];
if (value == self) {
value = [NSString stringWithFormat:
@"<%@ %p>",
NSStringFromClass([self class]),
self];
}
else if (value == nil) {
value = [NSNull null];
}
[dictionary setObject:value forKey:name];
}
free(properties);
return [NSString stringWithFormat:
@"<%@ %p> %@",
NSStringFromClass([self class]),
self,
dictionary];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment