Skip to content

Instantly share code, notes, and snippets.

@alexshafran
Last active August 29, 2015 13:55
Show Gist options
  • Save alexshafran/8693166 to your computer and use it in GitHub Desktop.
Save alexshafran/8693166 to your computer and use it in GitHub Desktop.
Property Names
#import <Foundation/Foundation.h>
@interface NSObject (PropertyNames)
+ (NSArray *)propertyNames;
+ (NSArray *)propertyNamesRecursive:(BOOL)recursive;
@end
#import "NSObject+PropertyNames.h"
#import <Foundation/NSObjCRuntime.h>
#import <objc/runtime.h>
@implementation NSObject (PropertyNames)
+ (NSArray *)propertyNames
{
return [self propertyNamesRecursive:NO];
}
+ (NSArray *)propertyNamesRecursive:(BOOL)recursive
{
NSMutableArray *propertyNames = [@[] mutableCopy];
unsigned int outCount, i;
objc_property_t *propertyList = class_copyPropertyList(self, &outCount);
for (i = 0; i < outCount; i++)
{
objc_property_t property = propertyList[i];
NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property)
encoding:NSUTF8StringEncoding];
[propertyNames addObject:propertyName];
}
free(propertyList);
Class superclass = class_getSuperclass(self);
if (superclass && recursive)
{
NSArray *superProperties = [superclass propertyNamesRecursive:YES];
return [propertyNames arrayByAddingObjectsFromArray:superProperties];
}
else
{
return propertyNames;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment