title: Assign Enum to Variable in Objective-C
body: <p>How can I assign an <a href="/questions/tagged/enum" class="post-tag" title="show questions tagged &#39;enum&#39;" rel="tag">enum</a> to a variable and access its value later? I thought this would be pretty simple, but every time I try to assign the <code>enum</code> value to a variable (no type mismatches or warnings in Xcode appear) my app crashes with an <strong>EXC_BAD_ACCESS</strong> error.</p>
  
  <p>Here's how I setup my <code>enum</code> in my header file (<code>BarTypes.h</code>):</p>
  
  <pre><code>typedef enum {
      BarStyleGlossy,
      BarStyleMatte,
      BarStyleFlat
  } BarDisplayStyle;
  </code></pre>
  
  <p>No issues there (reading and using the values at least). However, when I create a variable that can store one of the <code>enum</code> values (<em>BarStyleGlossy</em>, <em>BarStyleMatte</em>, or <em>BarStyleFlat</em>) then try to set that variable, the app crashes. Here's how I setup and use the variable:</p>
  
  <pre><code>//Header
  @property (nonatomic, assign, readwrite) BarDisplayStyle barViewDisplayStyle; //I've also tried just using (nonatomic) and I've also tried (nonatomic, assign)
  
  //Implementation
  @synthesize barViewDisplayStyle;
  
  - (void)setupBarStyle:(BarDisplayStyle)displayStyle {
      //This is where it crashes:
      self.barViewDisplayStyle = displayStyle;
  }
  </code></pre>
  
  <p>Why is it crashing here? <strong>How do I store the value of an enum in a variable?</strong> I think the issue has to do with a lack of understanding about <code>enums</code> on my end, however if I follow conventional variable setup and allocation, etc. this should work. Any ideas on what I'm doing wrong?</p>
  
  <p>Please note that I'm new to <code>enum</code>s, so my vocabulary here may be a bit mixed up (forgive me - and feel free to make an edit if you know what I'm trying to say).</p>
  
  <p>I found a few references about <code>enums</code> across the web:  </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/707512/what-is-a-typedef-enum-in-objective-c">What is a typedef enum in Objective-C?</a></li>
  <li><a href="https://stackoverflow.com/questions/1627501/using-enum-types-as-properties-in-objective-c">Using enum types as properties in Objective C</a></li>
  <li><a href="https://stackoverflow.com/questions/3606814/how-to-create-global-enum">How to create global enum</a></li>
  <li><a href="https://stackoverflow.com/questions/2212080/how-do-i-define-and-use-an-enum-in-objective-c">How do I define and use an ENUM in Objective-C?</a></li>
  <li>I also tried searching <a href="https://developer.apple.com" rel="nofollow noreferrer">Apple's Developer site</a> but only came up with results about types for Apple APIs (ex. Foundation, UIKit, etc.)</li>
  </ul>
  
  <p>EDIT: Here's how I call the <code>setupBarStyle</code> method:</p>
  
  <pre><code>BarView *bar = [[BarView alloc] init];
  [bar setupBarStyle:displayStyle];
  </code></pre>