Casebash
Casebash

Reputation: 118832

Attribute introspection to get a variable in Objective-C

Given a variable id x and a string NSString *s how can I get the instance attribute with name s for variable x?

ie. If we write NSString *s=@"a", then we want x.a

Upvotes: 3

Views: 4010

Answers (2)

Jon Reid
Jon Reid

Reputation: 20980

The Objective-C Runtime Reference lists

Ivar class_getInstanceVariable(Class cls, const char * name)

which returns an opaque type representing an instance variable in a class. You then pass that to

id object_getIvar(id object, Ivar ivar)

to get the actual instance variable. So you could say

#import <objc/runtime.h>

id getInstanceVariable(id x, NSString * s)
{
    Ivar ivar = class_getInstanceVariable([x class], [s UTF8String]);
    return object_getIvar(x, ivar);
}

if the instance variable is an object. However, if the instance variable is not an object, call

Ivar object_getInstanceVariable(id obj, const char * name, void ** outValue)

passing in a pointer to a variable of the right type. For example, if the instance variable is an int,

int num;
object_getInstanceVariable(x, [s UTF8String], (void**)&num);

will set num to the value of the integer instance variable.

Upvotes: 15

Rob Keniger
Rob Keniger

Reputation: 46020

Providing that x is key-value coding compliant for the a property, you can just do this:

id result = [x valueForKey:s]

Upvotes: 5

Related Questions