title: IOS Count current streak of days
body: <p>I'm making a day streak counter using UserDefaults and Core Data.</p>
  <p>The idea is that a number will be added unto by 1 every separate day an action is performed-- this will be the streak number.
  If this action wasn't performed for 24 hours, the number would reset to zero.</p>
  <p>I have a function to set the end of the streak:</p>
  <pre><code> // set date time to the end of the day so the user has 24hrs to add to the streak
   func changeDateTime(userDate: NSDate) -&gt; NSDate {
      let dateComponents = NSDateComponents()
      let currentCalendar = NSCalendar.current
      let year = Int(currentCalendar.component(NSCalendar.Unit.Year, fromDate: 
      userDate))
      let month = Int(currentCalendar.component(NSCalendar.Unit.Month, fromDate: 
      userDate))
      let day = Int(currentCalendar.component(NSCalendar.Unit.Day, fromDate: userDate))
  
      dateComponents.year = year
      dateComponents.month = month
      dateComponents.day = day
      dateComponents.hour = 23
      dateComponents.minute = 59
      dateComponents.second = 59
  
      guard let returnDate = currentCalendar.dateFromComponents(dateComponents) else {
          return userDate
      }
      return returnDate
   }
  </code></pre>
  <p>It is returning the following Errors:</p>
  <blockquote>
  <p>'NSDate' is not implicitly convertible to 'Date'; did you mean to use
  'as' to     explicitly convert?</p>
  <p>Cannot convert value of type 'NSCalendar.Unit' to expected argument
  type     'Calendar.Component'</p>
  </blockquote>
  <p>When using the suggested corrections I only get more errors with no suggested corrections. I'm having trouble figuring out the proper way to express this</p>
  <p>The full Code is:</p>
  <pre><code>let userDefaults = UserDefaults.standard
  var moc: NSManagedObjectContext!
  
  var lastStreakEndDate: NSDate!
  var streakTotal: Int!
  
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
  
      // checks for object if nil creates one (used for first run)
      if userDefaults.object(forKey: &quot;lastStreakEndDate&quot;) == nil {
          userDefaults.set(NSDate(), forKey: &quot;lastStreakEndDate&quot;)
      }
  
      lastStreakEndDate = (userDefaults.object(forKey: &quot;lastStreakEndDate&quot;) as! NSDate)
  
      streakTotal = calculateStreak(lastDate: lastStreakEndDate)
  }
  
  
  
  
  
  // fetches dates since last streak
  func fetchLatestDates(moc: NSManagedObjectContext, lastDate: NSDate) -&gt; [NSDate] {
      var dates = [NSDate]()
  
      let fetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: &quot;streakCount&quot;)
      let datePredicate = NSPredicate(format: &quot;date &lt; %@&quot;, lastDate)
  
      fetchRequest.predicate = datePredicate
  
      do {
          let result = try moc.fetch(fetchRequest)
          let allDates = result as! [NSDate]
          if allDates.count &gt; 0 {
              for date in allDates {
                  dates.append(date)
              }
          }
      } catch {
          fatalError()
      }
      return dates
  }
  
  
  // set date time to the end of the day so the user has 24hrs to add to the streak
  func changeDateTime(userDate: NSDate) -&gt; NSDate {
      let dateComponents = NSDateComponents()
      let currentCalendar = NSCalendar.current
      let year = Int(currentCalendar.component(NSCalendar.Unit.Year, fromDate: userDate))
      let month = Int(currentCalendar.component(NSCalendar.Unit.Month, fromDate: userDate))
      let day = Int(currentCalendar.component(NSCalendar.Unit.Day, fromDate: userDate))
  
      dateComponents.year = year
      dateComponents.month = month
      dateComponents.day = day
      dateComponents.hour = 23
      dateComponents.minute = 59
      dateComponents.second = 59
  
      guard let returnDate = currentCalendar.dateFromComponents(dateComponents) else {
          return userDate
      }
      return returnDate
  }
  
  
  
  
  
  // adds a day to the date
  func addDay(today: NSDate) -&gt; NSDate {
      let tomorrow = NSCalendar.currentCalendar.dateByAddingUnit(.Day, value: 1, toDate: today, options: NSCalendar.Options(rawValue: 0))
  
      return tomorrow!
  }
  
  
  
  // this method returns the total of the streak and sets the ending date of the last streak
  func calculateStreak(lastDate: NSDate) -&gt; Int {
      let dateList = fetchLatestDates(moc: moc, lastDate: lastDate)
      let compareDate = changeDateTime(userDate: lastDate)
      var streakDateList = [NSDate]()
      var tomorrow = addDay(today: compareDate)
  
      for date in dateList {
          changeDateTime(userDate: date)
          if date == tomorrow {
             streakDateList.append(date)
          }
          tomorrow = addDay(today: tomorrow)
      }
  
      userDefaults.set(streakDateList.last, forKey: &quot;lastStreakEndDate&quot;)
      return streakDateList.count
  }
  </code></pre>
  <p>Any Help is Appreciated</p>