Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As htw said, it's a visibility modifier. <code>@private</code> means that the ivar (instance variable) can only be accessed directly from within an instance of that same class. However, that may not mean much to you, so let me give you an example. We'll use the <code>init</code> methods of the classes as examples, for simplicity's sake. I'll comment inline to point out items of interest.</p> <pre><code>@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 </code></pre> <hr> <pre><code>@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; } </code></pre> <hr> <pre><code>@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-&gt;publicNumber = 42; // If we try to use direct access on any other ivars, // the compiler won't let us other-&gt;protectedLetter = 'M'; // COMPILER ERROR HERE other-&gt;privateBool = YES; // COMPILER ERROR HERE other-&gt;secondClassCitizen = 1.2; // COMPILER ERROR HERE } return self; } </code></pre> <p>So to answer your question, @private protects ivars from access by an instance of any other class. Note that two instances of MyFirstClass could access all of each other's ivars directly; it is assumed that since the programmer has complete control over this class directly, he will use this ability wisely.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload