title: Why do we always assign Objective-C primitives?
body: <p>It's common knowledge that we should use the attribute <code>(assign)</code> on primitive properties in Objective-C, yet I've never had a clear explanation as to why we do this. </p>
  
  <p>consider</p>
  
  <pre><code>[myObject setIntProperty:22];
  </code></pre>
  
  <p>for class <code>myObject</code> that has a <code>@property (nonatomic, assign) int intProperty;</code></p>
  
  <p>My imagination says we would be assigning <code>intProperty</code> a pointer to stack memory that holds the value 22. So since that pointer has to live on for our property to have any value, does this mean that every primitive has an unlimited lifetime in stack memory?</p>
  
  <p>Some more code to illustrate what I don't understand:</p>
  
  <p>MyObject.h ----</p>
  
  <pre><code>@interface MyObject : NSObject
  @property (nonatomic, assign) int myInt;
  @property (nonatomic, assign) NSString* myString;
  @end
  </code></pre>
  
  <p>OtherFile.m ----</p>
  
  <pre><code>-(void) doSomething
  {
     int x = 22;
     NSString* str = @"something";
  
     MyObject* obj = [[MyObject alloc] init];
  
     obj.myInt = x;
     obj.myString = str;
  
     [self doSomethingInOtherFunctionWith:obj];
  }
  </code></pre>
  
  <p>Now once the <code>doSomething</code> function has completed and moved on, the assigned string myString now points to nothing, since it shared a pointer with the function's local <code>str,</code> which is destroyed after the function runs. But myInt remains as 22. </p>
  
  <p>I understand that this is the way it works, being passed by value, and I do not question HOW it works, just WHY. Doesn't an int have to be a pointer at some level? </p>