Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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