How to print the values ​​of all declared properties of an NSObject?

I want to override the object description method so that I can print the values ​​of all my declared properties. I know that I can do this by adding all the values ​​to each other, one by one, but sometimes it takes a long time if you have many properties.

I was wondering if there is an easy way to do this, getting help from Objective-C runtime permissions?

+6
source share
1 answer

Have you tried this solution ?

#import <Foundation/Foundation.h> #import <objc/runtime.h> @interface NSObject (PropertyListing) // aps suffix to avoid namespace collsion // ...for Andrew Paul Sardone - (NSDictionary *)properties_aps; @end @implementation NSObject (PropertyListing) - (NSDictionary *)properties_aps { NSMutableDictionary *props = [NSMutableDictionary dictionary]; unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); for (i = 0; i < outCount; i++) { objc_property_t property = properties[i]; NSString *propertyName = [[[NSString alloc] initWithCString:property_getName(property)] autorelease]; id propertyValue = [self valueForKey:(NSString *)propertyName]; if (propertyValue) [props setObject:propertyValue forKey:propertyName]; } free(properties); return props; } @end 
+6
source

All Articles