Initially called method object_getinstancevariable (ID obj, const char *name, void**outvalue), use this method to actually get the value of the private variable to another class , but can only be used in non-ARC environments (configuration method: Click on the project name->build settings-> search Automatic Reference counting), my project is arc, certainly not, so I continue to find ways.
So I found this method Class_getinstancevariable (class CLS, const char *name), by this method, you can get the private variables of a class, and then through this method object_ Getivar (ID obj, Ivar Ivar) can get the value of the private variable, test it, no problem, it's done.
Member variable @interface parent:nsobject { nsstring *string;}-(ID) init { if (self = [super init]) { string = @ "Hello you"; } return self; }//Remember import header file #import <objc/runtime.h> @interface child:nsobject@end-(void) Child { nsstring *str; Parent *obj = [[[Parent alloc] init]; Ivar Ivar = class_getinstancevariable ([Parent class], "string"); str = Object_getivar (obj, Ivar); Unrestricted, return value ID type //object_getinstancevariable (obj, "string", (void *) &str); Non-Arc use method, no return value NSLog (@ "%@", str);}
Gets the private variable of the parent class