While I totally agree with Yuji, and KVC is the correct approach for the vast majority of problems this question probably relates too, in the interest of the small number of cases where you really do want to go poking around in the ivars, the answer is to use object_getInstanceVariable()
for this. For example:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface MyObject : NSObject {
double _someDouble;
}
@property double someDouble;
@end
@implementation MyObject
@synthesize someDouble=_someDouble;
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MyObject *object = [[MyObject alloc] init];
object.someDouble = 3.14;
double doubleOut;
object_getInstanceVariable(object, "_someDouble", (void**)&doubleOut);
NSLog(@"%f", doubleOut);
[object release];
[pool drain];
return 0;
}
Note the bizarre cast of double*
to a void**
. It is correct, required to avoid warnings, and is a side effect of the usual use case where this returns an object pointer.
But you really shouldn't be poking around in the ivars in all but a very few cases.