Skip to content

Instantly share code, notes, and snippets.

@Grayson
Created April 21, 2009 01:34
Show Gist options
  • Save Grayson/98879 to your computer and use it in GitHub Desktop.
Save Grayson/98879 to your computer and use it in GitHub Desktop.
// Simple way of getting a list of ivar names for an Objective-C class.
#import <objc/runtime.h>
@interface NSObject (ivars)
-(NSArray *)ivars {
unsigned int ivarCount = 0;
Ivar *ivars = class_copyIvarList([self class], &ivarCount);
if (ivars && ivarCount) {
NSMutableArray *array = [NSMutableArray array];
unsigned int idx = 0;
for (idx=0; idx < ivarCount; idx++) {
Ivar ivar = ivars[idx];
[array addObject:[NSString stringWithUTF8String:ivar_getName(ivar)]];
}
free(ivars);
return array;
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment