Skip to content

Instantly share code, notes, and snippets.

@necrowman
Created February 14, 2019 13:33
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 necrowman/7ca04d1038ec9315e6c5a95a41211792 to your computer and use it in GitHub Desktop.
Save necrowman/7ca04d1038ec9315e6c5a95a41211792 to your computer and use it in GitHub Desktop.
Adding full description with properties list and their values... For Debugging Objective-C classes;
//
// NSObject+FullDescription.h
// DebuggingTools
//
// Created by Ruslan Yupyn on 2/14/19.
// Copyright © 2019 necrowman. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSObject(FullDescription)
- (NSArray *)allPropertyNames;
@end
NS_ASSUME_NONNULL_END
//
// NSObject+FullDescription.m
// DebuggingTools
//
// Created by Ruslan Yupyn on 2/14/19.
// Copyright © 2019 necrowman. All rights reserved.
//
#import "NSObject+FullDescription.h"
#import <objc/runtime.h>
@implementation NSObject(FullDescription)
- (NSArray *)allPropertyNames {
uint count;
objc_property_t* properties = class_copyPropertyList(self.class, &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++) {
const char* propertyName = property_getName(properties[i]);
[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
}
free(properties);
return propertyArray;
}
- (NSString *)description {
NSMutableString * propertiesString = [NSMutableString stringWithFormat:@"%@ {\nproperties list:\n", [self class]];
NSArray * allPropertyNames = [self allPropertyNames];
for (NSString * prop in allPropertyNames) {
[propertiesString appendFormat:@"\t%@,\n", prop];
}
[propertiesString appendString:@"}\n\nvalues:\n["];
for (NSString * prop in allPropertyNames) {
id attribute = [self valueForKey:prop];
if([attribute isKindOfClass:[NSData class]]) {
[propertiesString appendFormat:@"\n\t%@ : <NSData> %ld bytes,", prop, sizeof(attribute)];
} else {
[propertiesString appendFormat:@"\n\t%@ : %@,", prop, attribute];
}
}
[propertiesString appendFormat:@"\n]\n"];
return propertiesString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment