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
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
Best Answer
The Objective-C Runtime Reference lists
which returns an opaque type representing an instance variable in a class. You then pass that to
to get the actual instance variable. So you could say
if the instance variable is an object. However, if the instance variable is not an object, call
passing in a pointer to a variable of the right type. For example, if the instance variable is an int,
will set
num
to the value of the integer instance variable.