jamztang
8/27/2010 - 3:58 PM

NSObject+PropertyListing.h

//
//  NSObject+PropertyListing.m
//  PropertyFun
//
//  Created by Andrew Sardone on 8/27/10.
//

#import "NSObject+PropertyListing.h"

#import <objc/runtime.h>

@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
//
//  NSObject+PropertyListing.h
//  PropertyFun
//
//  Created by Andrew Sardone on 8/27/10.
//

#import <Foundation/Foundation.h>


@interface NSObject (PropertyListing)

// aps suffix to avoid namespace collsion
//   ...for Andrew Paul Sardone
- (NSDictionary *)properties_aps;

@end