Objective-CLearning notes acquisitionPrivate variableThe value is the content to be introduced in this article. It mainly describes how to obtainPrivate variableFor more information, see.
1. The following is a self-defined class with a private variable mt _ and the initial value is "Ha ".
- @interface Mobj : NSObject {
- @private
- NSString *mt_;
- }
- @end
-
- @implementation Mobj
- - (id)init {
- self = [super init];
- if (self) {
- mt_ = [[NSString alloc] initWithString:@"Ha Ha Ha"];
- }
- return self;
- }
- - (void) dealloc {
- [mt_ release];
- [super dealloc];
- }
- @end
2. The following code is how to get the private variable. Remember to add the header file # import <objc/runtime. h> ):
- NSString *str;
- Mobj *obj = [[Mobj alloc] init];
- object_getInstanceVariable(obj, "mt_", (void *)&str);
- NSLog(@"%@",str);
- [obj release];
3. output result:
- Ha Ha Ha
OK.Private variable.
Summary:Objective-CLearning notes acquisitionPrivate variableThe content of the value is complete. I hope this article will help you!