Created
June 30, 2013 02:20
-
-
Save levey/5893530 to your computer and use it in GitHub Desktop.
Detail Description for NSObject
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
@interface NSObject(DetailDescription) | |
- (NSString *)detailDescription; | |
@end |
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 "NSObject+DetailDescription.h" | |
#import <objc/runtime.h> | |
@implementation NSObject(DetailDescription) | |
- (NSString *)detailDescriptionForClassType:(Class)classType { | |
NSMutableString *properties = [NSMutableString string]; | |
// Find Out something about super Classes | |
Class superClass = class_getSuperclass(classType); | |
if (superClass != nil && ![superClass isEqual:[NSObject class]]) | |
{ | |
// Append all the super class's properties to the properties (Reqursive, until NSObject) | |
[properties appendString:[self detailDescriptionForClassType:superClass]]; | |
} | |
// Add Information about Current Properties | |
NSUInteger property_count; | |
objc_property_t * property_list = class_copyPropertyList(classType, &property_count); // Must Free, later | |
for (int i = property_count - 1; i >= 0; --i) { // Reverse order, to get Properties in order they were defined | |
objc_property_t property = property_list[i]; | |
// For Eeach property we are loading its name | |
const char * property_name = property_getName(property); | |
NSString * propertyName = [NSString stringWithCString:property_name encoding:NSASCIIStringEncoding]; | |
if (propertyName) { // and if name is ok, we are getting value using KVC | |
id value = [self valueForKey:propertyName]; | |
// format of properties items: p1 = v1; p2 = v2; ... | |
[properties appendFormat:@"%@ = %@; ", propertyName, value]; | |
} | |
} | |
free(property_list);//Clean up | |
return properties; | |
} | |
- (NSString *)detailDescription { | |
return [NSString stringWithFormat:@"[%@ {%@}]", NSStringFromClass([self class]), [self detailDescriptionForClassType:[self class]]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment