Paolo Paolo - 8 months ago 28
Objective-C Question

Objective-C: accessing instance variables declared as @private

An external framework I'm using in my application defines

whose internal structure is opaque.

Its instance variables are not meant to be accessed directly.

@class TheClassPrivateVars;

@interface TheClass : NSObject
TheClassPrivateVars *_theClassPriv;


The framework public interface is not as complete as it should be and (at my I own risk) I want to read one of the private variables of

Fortunately the framework is supplied with complete source code so I have access to the definition of

@interface TheClassPrivateVars : NSObject
int thePrivateInstanceVar; // I want to read this!

// other properties here...


I've made a header file with the above code and included it just in the source file where the "abusive access" have to happen.

theValue = instanceOfTheClass->_theClassPriv->thePrivateInstanceVar;

is declared as

Is there any way I can get around it without modifying the original header file ?

TheClassPrivateVars* private = [instanceOfTheClass valueForKey:@"_theClassPriv"];

EDIT: or using key path:

theValue = [[instanceOfTheClass valueForKeyPath:"_theClassPriv.thePrivateProperty"] integerValue];