Skip to content

Instantly share code, notes, and snippets.

@mkuliszkiewicz
Last active December 19, 2015 21:18
Show Gist options
  • Select an option

  • Save mkuliszkiewicz/6019056 to your computer and use it in GitHub Desktop.

Select an option

Save mkuliszkiewicz/6019056 to your computer and use it in GitHub Desktop.
//
// NSObject+LogProperties.h
//
// Gist created by Maciej Banasiewicz.
// Log all properties of an object.
// Credits: Tieme
// Source: http://stackoverflow.com/questions/13922581/is-there-a-way-to-log-all-the-property-values-of-an-objective-c-instance
#import <Foundation/Foundation.h>
@interface NSObject (LogProperties)
- (void)logProperties;
- (NSString *)propertiesString;
@end
//
// NSObject+LogProperties.h
//
// Gist created by Maciej Banasiewicz.
// Log all properties of an object.
// Credits: Tieme
// Source: http://stackoverflow.com/questions/13922581/is-there-a-way-to-log-all-the-property-values-of-an-objective-c-instance
#import "NSObject+LogProperties.h"
#import <objc/runtime.h>
@implementation NSObject (LogProperties)
- (NSString *) propertiesString
{
NSMutableString *ret = [NSMutableString string];
[ret appendString:[NSString stringWithFormat:@"----------------------------------------------- Properties for object %@\n", [self class]]];
@autoreleasepool {
unsigned int numberOfProperties = 0;
objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties);
for (NSUInteger i = 0; i < numberOfProperties; i++) {
objc_property_t property = propertyArray[i];
NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
[ret appendFormat:@"Property %@ Value: %@\n", name, [self valueForKey:name]];
}
free(propertyArray);
}
[ret appendString:@"-----------------------------------------------\n"];
return ret;
}
- (void)logProperties
{
NSLog(@"%@", [self propertiesString]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment