title: Creating and setting typedef struct values in Objective-C
body: <p>I have a class in a game that is often used, and I thought it would be nice to tidy it up by grouping together instance variables with a typedef struct. I'm not completely convinced yet this will help or not. </p>
  
  <p><strong>Originally in my class header interface I had something like this:</strong></p>
  
  <pre><code>@interface ThingClass : CCLayer {
  
       @public
  
            bool _invulnerableToggled;
            int _invulnerableCount;
            int _invulnerableMax;
  
  }
  
  @property(nonatomic, assign) bool invulnerableToggled;
  @property(nonatomic, assign) int invulnerableCount;
  @property(nonatomic, assign) int invulnerableMax;
  </code></pre>
  
  <p>and in my .m</p>
  
  <pre><code>@synthesize
  
  invulnerableToggled = _invulnerableToggled,
  invulnerableCount = _invulnerableCount,
  invulnerableMax = _invulnerableMax;
  </code></pre>
  
  <p>A subclass of this class would set these variables to their default values in init.  Another class could access an instance of this subclass and set the values accordingly with regular dot notation, like tempThing.invulnerableToggled = YES;</p>
  
  <p>Now that I'm using a typedef struct, it looks as though my values cannot be adjusted, and I've tried various things to overcome it. Although it may be because I'm not setting this up correctly to begin with, so just in case I'll show you what I'm doing now.</p>
  
  <p><strong>Currently my class header:</strong></p>
  
  <pre><code>typedef struct {
  
      bool toggled;
      int count;
      int max;
  
  } Invulnerable;
  
  @interface ThingClass : CCLayer {
  
       @public
            Invulnerable _invulnerable;
  
  }
  
  @property(nonatomic, assign) Invulnerable invulnerable;
  </code></pre>
  
  <p>and in my .m</p>
  
  <pre><code>@synthesize
  
  invulnerable = _invulnerable;
  </code></pre>
  
  <p>I set these values in a subclass init like so:</p>
  
  <pre><code>_invulnerable.toggled = NO;
  _invulnerable.count = 0;
  _invulnerable.max = 50;
  </code></pre>
  
  <p>When I try to set this in another class, I expect it to add 1 to the current value.  It always remains 1 instead.  This if statement is sometimes checked 60 times a second, but has not change to the count:</p>
  
  <pre><code>Invulnerable invulnerable = tempBaddy.invulnerable;
  
  // check baddy invulnerability and increment if needed
  
  if(invulnerable.toggled == YES){
  
      int increase = invulnerable.count +1;
  
      invulnerable.count = increase;
  
      NSLog(@"invulnerable.count = %i", invulnerable.count);
  }
  </code></pre>