ref – http://stackoverflow.com/questions/844658/what-does-private-mean-in-objective-c
The @public, @protected, @private are called accessor modifiers. They provide access visibility to the instance variable.
So we declare 3 iVar using @public, @protected, @private.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@interface MyFirstClass : NSObject { @public int publicNumber; @protected // Protected is the default char protectedLetter; @private BOOL privateBool; } @end @implementation MyFirstClass - (id)init { if (self = [super init]) { publicNumber = 3; protectedLetter = 'Q'; privateBool = NO; } return self; } @end |
We then declare a child class, and declare MyFirstClass as a parent class.
The basics are laid out here.
- @public iVars CAN be accessed by the child class
- @protected iVars CAN be accessed by the child class
- @private iVars CANNOT BE accessed by the child class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
@interface MySecondClass : MyFirstClass // Note the inheritance { @private double secondClassCitizen; } @end @implementation MySecondClass - (id)init { if (self = [super init]) { // We can access publicNumber because it's public; // ANYONE can access it. publicNumber = 5; // We can access protectedLetter because it's protected // and it is declared by a superclass; @protected variables // are available to subclasses. protectedLetter = 'z'; // We can't access privateBool because it's private; // only methods of the class that declared privateBool // can use it privateBool = NO; // COMPILER ERROR HERE // We can access secondClassCitizen directly because we // declared it; even though it's private, we can get it. secondClassCitizen = 5.2; } return self; } |
Since we do not provide any accessor methods via properties, the instance varaibles are not accessed via the dot notation (.)
Rather, they are accessed through the arrow ( -> ).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@interface SomeOtherClass : NSObject { MySecondClass *other; } @end @implementation SomeOtherClass - (id)init { if (self = [super init]) { other = [[MySecondClass alloc] init]; // Neither MyFirstClass nor MySecondClass provided any // accessor methods, so if we're going to access any ivars // we'll have to do it directly, like this: other->publicNumber = 42; // If we try to use direct access on any other ivars, // the compiler won't let us other->protectedLetter = 'M'; // COMPILER ERROR HERE other->privateBool = YES; // COMPILER ERROR HERE other->secondClassCitizen = 1.2; // COMPILER ERROR HERE } return self; } |
enforce the use of accessors
Say a developer writes a class, then write properties and accessors. However, what if some other developer goes
1 |
yourObject->someVar; //UH OH! |
xcode 4.x+ automatically puts @private in the template for an object. Thus, you don’t have to worry about other people accessing your instance variable with the arrow and modifying unexpectedly.
The only way to modify the instance variables, is through your property methods.