Skip to content

Instantly share code, notes, and snippets.

@leviathan
Created January 20, 2016 09:10
Show Gist options
  • Save leviathan/4e3237b705ba88b6780a to your computer and use it in GitHub Desktop.
Save leviathan/4e3237b705ba88b6780a to your computer and use it in GitHub Desktop.
Find properties as strings of a Class
/**
* Determines the properties of @c className and returns list of these properties as strings.
* The method will iterate through @c className superclass hierarchy and fetch the properties from
* the superclass hierarchy as well.
*
* @return the list of properties found on @c className
*/
- (NSOrderedSet<NSString *> *)getMethodListForClass:(Class)className
{
NSMutableOrderedSet<NSString *> *propertiesList = [NSMutableOrderedSet orderedSet];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(className, &outCount);
for (i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
const char *propName = property_getName(property);
if (propName)
{
NSString *propertyName = [NSString stringWithCString:propName encoding:[NSString defaultCStringEncoding]];
[propertiesList addObject:propertyName];
// Create the property's setter method name
NSString *propertySetterName = [NSString stringWithFormat:@"set%@", [propertyName capitalizedString]];
[propertiesList addObject:propertySetterName];
// Create the property's getter method name
NSString *propertyGetterName = [NSString stringWithFormat:@"get%@", [propertyName capitalizedString]];
[propertiesList addObject:propertyGetterName];
}
}
free(properties);
// When superclass is present, then recurse into superclass structure
Class superClass = class_getSuperclass(className);
if (superClass)
{
NSOrderedSet<NSString *> *methodsList = [self getMethodListForClass:superClass];
// add the properties, which are not
[propertiesList unionSet:[methodsList set]];
}
return propertiesList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment