Gets the private variable of the parent class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.