title: NSDateComponents / NSDate : get the last sunday
body: <p>I know there are some questions similar to this one, but I just can't find what's wrong with my code.</p>
  
  <p>Basically what I want is if today is not sunday, currentDate is set to the last sunday.
  If today is sunday I currentDate is set to the sunday before.</p>
  
  <p>Here's how I'm trying to do it.</p>
  
  <pre><code>  NSDateComponents *components = [[NSCalendar currentCalendar] components:  NSWeekCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit fromDate:currentDate];
  
      if (components.weekday == 1) { //Its sunday. We just need to subtract a week
          [components setWeek: -1];
          currentDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:currentDate options:0];
  
      }else{ //If its not sunday we just go to sunday
          [components setWeekday: 1];
          currentDate = [[NSCalendar currentCalendar] dateFromComponents:components];
  
      }
  </code></pre>
  
  <p>The first time this part of the code is executed I get the right answer. After the first time I get weird dates, like 02 Dec. 4026, and the year keeps going up.</p>
  
  <p>Here's the code that made it work:</p>
  
  <pre><code>   NSCalendar *calendar = [NSCalendar currentCalendar];
      NSDateComponents *components = [calendar components: ( NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit) fromDate:currentDate];
      int weekday = components.weekday;
  
      if (weekday == 1) {
          [components setWeek:components.week -1];
      }else{
          [components setWeekday:1];
      }
      currentDate = [calendar dateFromComponents:components];
  </code></pre>