Objective-C: access to instance variables declared as @private

The external framework that I use in my application determines theClasswhich internal structure is opaque.

Its instance variables are not intended for direct access.

@class TheClassPrivateVars;

@interface TheClass : NSObject
{
    @private
    TheClassPrivateVars *_theClassPriv;
}

@end

The open interface of the frame is not as complete as it should be, and (at my own risk) I want to read one of the private variables myClass.

Fortunately, the framework comes with full source code, so I have access to the definition TheClassPrivateVars:

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

    //
    // other properties here...
    //
}

@end

I created a header file with the above code and included it only in the source file where "abusive access" should occur.

theValue = instanceOfTheClass->_theClassPriv->thePrivateInstanceVar;

Sorry _theClassPrivdeclared as @private.

?

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

: :

theValue = [[instanceOfTheClass valueForKeyPath:"_theClassPriv.thePrivateProperty"] integerValue];
+14
#import <objc/runtime.h>

TheClassPrivateVars *_data;
object_getInstanceVariable(self, "_theClassPriv", (void**)&_data);

?

+4

It would probably be a good idea if it were Apple's infrastructure to tell us which infrastructure and what property you are talking about, for some educated guesses, it is exactly how stupid or not to access this private property. In the end, private property may leave tomorrow. And you won’t be able to easily get your app on the App Store.

-1
source

Source: https://habr.com/ru/post/1527314/


All Articles