Problem with subclass and superclass using the same properties

This is something very fascinating that I watched today. It is possible that Objective-C is working, but I did not know about it. See the following code below:

// ATableViewController.h @interface ATableViewController : UITableViewController @end // ATableViewController.m @interface ATableViewController () @property (nonatomic) int volly; @end @implementation ATableViewController - (void)viewDidLoad { [super viewDidLoad]; self.volly = 5; } @end // BTableViewController.h @interface BTableViewController : ATableViewController @end // BTableViewController.m @interface BTableViewController () @property (nonatomic) int volly; @end @implementation BTableViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%d", self.volly); // returns 5 } @end 

I am not sure why this is true. I understand that I passed the message "volly" to the object "I", which, in turn, probably looked at the value from the superclass, but should it not be initialized? Some explanations would be very helpful. Thanks.

EDIT: This is a big IMO problem. Given that I do not know any particular properties defined in the superclass, my own set of values ​​may turn out to be different.

For example, a developer can set the hasAppeared boolean flag to viewDidAppear: The same value will be set for my subclass instance in viewDidAppear: after calling [super viewDidAppear:] . This will be before I actually install it myself.

Currently, the solution is that I know the variable used by the superclass exactly, and I can avoid using the same value, but I think this is a bigger problem than it seems.

EDIT 2: The behavior is compatible with binary files (only with headers), as well as with frameworks in which the implementation is implemented.

+7
objective-c
source share
1 answer

I answer this without reading all the comments.

There is no problem. Both AViewController ( AVC ) and BViewController ( BVC ) have their own private properties named volly .

You have created an instance of BVC . He cannot see the volly property from his parent class (because it is private), only his own.

Now the fun begins.

The viewDidLoad method from the BVC called. It, in turn, calls [super viewDidLoad]; Which, of course, calls viewDidLoad from the AVC class. This method, in turn, calls self.volly = 5; .

The confusion seems to be with this line. Remember that self.volly = 5; is really a challenge:

 [self setVolly:5]; 

Both AVC and BVC have a (synthesized) setVolly: method. Since self is a pointer to an instance of a BVC object, the call to [self setVolly:5]; leads to a call to the setVolly: method in the BVC class, although it is called from a method in the AVC class.

Here is the code with some annotations:

Class "BVC":

 - (void)viewDidLoad { [super viewDidLoad]; // calls the method in `AVC` NSLog(@"%d", self.volly); // returns 5 } 

Class "AVC":

 - (void)viewDidLoad { [super viewDidLoad]; // calls the UITableViewController method // The following is really [self setVolly:5]; // Since "self" is a "BVC", the private "volly" property of // the "BVC" class is actually set here. // The private "volly" property of the "AVC" class will still be // 0 after this call. self.volly = 5; } 

In the end, the subclass does not use the private property of the parent class. The original premise in the question header is incorrect.

+2
source share

All Articles