Playing around with BMITime and the Stocks projects, I noticed some strange behavior with the default @property and @synthesize accessors. Well, maybe it’s expected in C, but it kind of blows the water out of data encapsulation. As an example, if I have a class BMITime with an ivar object - say an Employee emp - then from main, I can write:
Employee *garbage = [myBMITime emp]; // I’ve stolen your reference, ie the reference to the actual emp
garbage = null; // and trashed it
So any random object in main can steal the reference to a totally separate object’s supposedly protected object ivar and do whatever it wants to it without using any of the setter methods specified.
Basically it seems like the @synthesize getter method returns the actual address of the actual ivar, instead of a copy of that ivar. Once that address is out there, any runaway Object - even one that’s not a parent or child - can overwrite the ivars of the class.
There’s got to be a way to specify to @synthesize that getters should return a copy of the ivar object (stored in separate memory) instead of the address to the actual ivar? That seems like a significant break with OO to have the default be the other way? Wouldn’t you be forced to define your own getters to provide a copy and not a pointer to the actual object to avoid this issue?
Thanks in advance for any help.