开发者

Attribute introspection to get a variable in Objective-C

开发者 https://www.devze.com 2022-12-17 19:49 出处:网络
Given a variable id x and a string NSString *s how can I get the instance attribute with name s for variable x?

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


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.


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

id result = [x valueForKey:s]
0

精彩评论

暂无评论...
验证码 换一张
取 消