title: How do I calculating the difference between two dates in Objective-C?
body: <p>I'm working through a challenge in Objective-C Programming, The Big Nerd Ranch Guide, and I'm a little flummoxed by one of the challenges.</p>
  
  <blockquote>
    <p>Use two instances of NSDate to figure out how many seconds you have been alive. Hint: here is how you create a new date object from the year, month, etc.:</p>
  </blockquote>
  
  <p>So I need to the difference between now and my date of birth in seconds. Sounds good. Then the hint shows up:</p>
  
  <pre><code>@autoreleasepool {
      NSDateComponents *comps = [[NSDateComponents alloc] init];
      [comps setYear:1981];
      [comps setMonth:7];
      [comps setDay:12];
      [comps setHour:1];
      [comps setMinute:55];
      [comps setSecond:33];
  
      NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  
      NSDate *dateOfBirth = [g dateFromComponents:comps];
  </code></pre>
  
  <p>The first issue is I don't understand what <code>*g</code> is meant to be. I know it's a pointer to an NSCalendar object. But why do we need it? And what is <code>g</code> meant to stand for?</p>
  
  <p>The sample code then uses the <code>g</code> variable to grab a date. In another language, this would be as easy as <code>DateDiff(dateOne, dateTwo, interval)</code>. I'm not clear on why the Calendar object is necessary in the first place, and why we have to create date components to feed the object.</p>
  
  <p>This is all new to me, and I've worked with dynamic languages in the past. So a "dummies" like explanation would be great!</p>