Create a gist now

Instantly share code, notes, and snippets.

List declared properties for an Objective-C class
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NSObject (DeclaredProperties)
- (NSArray *)properties;
@interface TestClass : NSObject
@property (readonly) NSString *prop1;
@property (readonly) NSNumber *prop2;
@property (readonly) NSArray *prop3;
@implementation NSObject (DeclaredProperties)
- (NSArray *)properties
NSMutableArray *propList = [NSMutableArray array];
unsigned int numProps = 0;
unsigned int i = 0;
objc_property_t *props = class_copyPropertyList([TestClass class], &numProps);
for (i = 0; i < numProps; i++) {
NSString *prop = [NSString stringWithUTF8String:property_getName(props[i])];
[propList addObject:prop];
return [[propList copy] autorelease];
@implementation TestClass
- (NSString *)prop1
return @"prop1";
- (NSNumber *)prop2
return [NSNumber numberWithInteger:2];
- (NSArray *)prop3
return [NSArray arrayWithObject:@"prop3"];
int main(int argc, char **argv)
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
TestClass *t = [[TestClass alloc] init];
NSLog(@"Properties: %@", [t properties]);
[t release];
[pool release];
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment