Monday, July 28th, 2008

A few months ago, I spent a few days searching around to see if I could figure out how to determine the instance variables on a class in ObjC. I knew it was possible because Key-Value-Coding will do it if it can’t find the well-named accessor methods for a keypath. But all of my searching proved fruitless and I went with a different method of accomplishing my end goal.

Well, it looks like the answer has appeared in my feed reader on an entry from Matt Gallagher’s Cocoa With Love blog:Key Value Information.

I’ll just quote him (from the very end of his post):

Similarly, if you were interested in key value coding that may access instance variables directly, you could use class _getClassVariable to get any instance variables with the same name as the key.

The leading underscore on the method name indicates that it’s undocumented and internal to Apple’s implementation. So using it isn’t necessarily the smartest thing in the world. But sometimes, you’ve gotta do what you gotta do.

Now that I have a method name, I’ll probably resume my search for information on this technique and will obviously report any findings here.

Update: July 29, 2008

I did some poking around in the Objective-C 2.0 Runtime Reference and found the class_copyIvarList function which returns an array of Ivar types. Ivar is a struct containing fields for the ivar_name, ivar_type, and offset. Combined with object_getIvar, I think I would have been much closer to achieving my original goal.

I don’t know why I couldn’t find this before. I guess I was just looking in the wrong places.

Comments are currently closed for this post.