24

If I have a class, how can I list all its instance variable names?

eg:

@interface MyClass : NSObject {
    int myInt;
    NSString* myString;
    NSMutableArray* myArray;
}

I would like to get "myInt", "myString", and "myArray". Is there some way to perhaps get an array of names that I can iterate over?

I've tried searching the Objective-C documentation but couldn't find anything (and I'm not sure what this is called either).

Dumoko
  • 2,614
  • 3
  • 25
  • 34
  • Are you working from source code (in which case you need an Objective-C parser) or during runtime (in which case you can use the Objective-C runtime API)? – Barry Wark Jul 31 '09 at 17:45
  • Source code, ie. the variables will not change during runtime and are all defined in the source code. I'm trying to write a general function to write out to a plist. I know I can use NSDictionary to do that but I'd like to avoid manually adding code all over the place when I add a new class variable, hence the question. –  Jul 31 '09 at 17:51
  • It sounds then like you actually mean at run time. – Barry Wark Jul 31 '09 at 18:11
  • 1
    Yes you are right, runtime it is. I found this post http://lists.apple.com/archives/objc-language/2008/Aug/msg00176.html that "Firstly, ivars are designed for the object's internal usage." and seems to suggest that using ivars is not a good practice. Are there any pitfalls that I should be aware of? Is using this code going to make something break somewhere else? This is code for an iPhone program if that makes any difference. –  Jul 31 '09 at 18:21
  • I assume you want to restore the object after you save it to file? If so this is probably not going to work. Just restoring the internal state of an object isn't always enough, there's connections to other objects, notifications, timers, KVOs, delegates, etc. What you want to do is implement the NSCoding protocol in your objects. It's the correct way to archive/restore Cocoa/iOS objects to a file. – kubi May 11 '11 at 20:31
  • Of course, if you don't want to restore, then ignore my comment. :-D – kubi May 11 '11 at 20:31
  • For iteration through list of variables of NSManagedObject: http://stackoverflow.com/questions/28301226/how-to-list-variables-for-nsmanagedobject/28302500#28302500 – Aqib Mumtaz Feb 03 '15 at 15:26

3 Answers3

44

As mentioned, you can use the Objective-C runtime API to retrieve the instance variable names:

unsigned int varCount;

Ivar *vars = class_copyIvarList([MyClass class], &varCount);

for (int i = 0; i < varCount; i++) {
    Ivar var = vars[i];

    const char* name = ivar_getName(var);
    const char* typeEncoding = ivar_getTypeEncoding(var);

    // do what you wish with the name and type here
}

free(vars);
jhauberg
  • 1,410
  • 12
  • 20
5
#import <objc/runtime.h>


NSUInteger count;
Ivar *vars = class_copyIvarList([self class], &count);
for (NSUInteger i=0; i<count; i++) {
    Ivar var = vars[i];
    NSLog(@"%s %s", ivar_getName(var), ivar_getTypeEncoding(var));
}
free(vars);
Community
  • 1
  • 1
Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
0

Consider gen_bridge_metadata, which is intended for a completely different purpose, but can produce XML files from Objective-C header files.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101