Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here, you're declaring an instance variable named <code>aVar</code>:</p> <pre><code>@interface myclass : UIImageView { int aVar; } </code></pre> <p>You can now use this variable within your class:</p> <pre><code>aVar = 42; NSLog(@"The Answer is %i.", aVar); </code></pre> <p>However, instance variables are private in Objective-C. What if you need other classes to be able to access and/or change <code>aVar</code>? Since methods are public in Objective-C, the answer is to write an accessor (getter) method that returns <code>aVar</code> and a mutator (setter) method that sets <code>aVar</code>:</p> <pre><code>// In header (.h) file - (int)aVar; - (void)setAVar:(int)newAVar; // In implementation (.m) file - (int)aVar { return aVar; } - (void)setAVar:(int)newAVar { if (aVar != newAVar) { aVar = newAVar; } } </code></pre> <p>Now other classes can get and set <code>aVar</code> via:</p> <pre><code>[myclass aVar]; [myclass setAVar:24]; </code></pre> <p>Writing these accessor and mutator methods can get quite tedious, so in Objective-C 2.0, Apple simplified it for us. We can now write:</p> <pre><code>// In header (.h) file @property (nonatomic, assign) int aVar; // In implementation (.m) file @synthesize aVar; </code></pre> <p>...and the accessor/mutator methods will be automatically generated for us.</p> <p><strong>To sum up:</strong></p> <ul> <li><p><code>int aVar;</code> declares an instance variable <code>aVar</code></p></li> <li><p><code>@property (nonatomic, assign) int aVar;</code> declares the accessor and mutator methods for <code>aVar</code></p></li> <li><p><code>@synthesize aVar;</code> implements the accessor and mutator methods for <code>aVar</code></p></li> </ul>
    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. 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