Last active
December 10, 2015 06:08
-
-
Save calebd/4392732 to your computer and use it in GitHub Desktop.
A better NSObject description.
This file contains hidden or 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 { | |
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