Last active
August 29, 2015 13:55
-
-
Save alexshafran/8693166 to your computer and use it in GitHub Desktop.
Property Names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSObject (PropertyNames) | |
+ (NSArray *)propertyNames; | |
+ (NSArray *)propertyNamesRecursive:(BOOL)recursive; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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