Created
April 21, 2009 01:34
-
-
Save Grayson/98879 to your computer and use it in GitHub Desktop.
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
// 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