title: How do I write a swift function that will count the number of days without an entry?
body: <p>I am working on a project with NSManagedObjects where each object is a user entry that has a value and a date.  The app will be running a 7 day average and a 14 day average of the values the user enters.  I have already set it up to do the appropriate fetch requests, sum the values, and divide by 7 and 14, respectively. However, I am realizing that when the user first begins using the app these running average values will be very misleading, so I would like to set up a function that will evaluate the number of days out of the last 7 and 14 that do not have any entries so I can subtract that value from the denominator in these calculations.  I am a relative beginner and am having a hard time getting my head around how to write this function though, so any help would be appreciated.  Thanks in advance.</p>
  
  <p>Edit in response to Drekka:</p>
  
  <p>The code I'm working from is below. I'm sorry for the broad question but I can't quite figure out where to start with structuring a looping function for what I'm trying to do and I haven't been able to come up with any examples or analogues in the searching I've done.  Basically I'm pulling all of the values entered in the last seven days but I'm trying to figure out a way to evaluate for days where no values were entered within the span of this fetch request.</p>
  
  <pre><code>func sevenDayFetch() {
      guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
  
      let managedContext = appDelegate.persistentContainer.viewContext
  
      let fetchRequest = NSFetchRequest&lt;NSManagedObject&gt;(entityName: "Entry")
  
      var calendar = Calendar.current
      calendar.timeZone = NSTimeZone.local
  
      let sevenDaysAgo = calendar.date(byAdding: .day, value: -7, to: Date())
      let dateFrom = calendar.startOfDay(for: sevenDaysAgo!)
      let dateTo = Date()
  
      let fromPredicate = NSPredicate(format: "entryDate &gt; %@", dateFrom as NSDate)
      let toPredicate = NSPredicate(format: "entryDate &lt;= %@", dateTo as NSDate)
      let sevenDayPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [fromPredicate, toPredicate])
      fetchRequest.predicate = sevenDayPredicate
  
      do {
          entryArray = try managedContext.fetch(fetchRequest)
          var sevenDayArray: [Int] = []
          for i in entryArray as [NSManagedObject] {
              sevenDayArray.append(i.value(forKey: "Value") as! Int)
          }
          let sevenDaySum = sevenDayArray.reduce(0, +)
          let sevenDayAverage = sevenDaySum/7
  
          sevenDayAverageLabel.text = String(sevenDayAverage)
  </code></pre>