title: Inserting into Array and comparing Dates Swift iOS Code
body: <p>I am attempting to create an array that will store 365 integers, it must be filled completely. I am using Healthkit to figure out the users steps from a year back, hence the array size. Every integer represents 1 day.</p>
  
  <p>I have done this in android already and it worked perfectly, I got 365 integers back with 0's for the days with no steps, however, the problem is with iOS health kit I get nothing from days with no data, which I need. In order to do this I thought I would compare the date variable I get with the date of the current day + 1 and loop through the array to see if it find any matching cases, if not put a 0 into it at the end. </p>
  
  <p>So in order to do this I created an array of 365, at the line var ID = 0 is where I attempt to store the integers correctly into the array.  I am using Swift 4. </p>
  
  <pre><code>struct stepy {
    static var step = [365]
  }
  </code></pre>
  
  <p>This is where I enumerate through the stepData, first at var ID I attempt to compare the date I get in the enumerate loop with the current date (basically index 0 in the array, which represents the first day, the current day). </p>
  
  <p>However I got a problem, currently I believe I would overwrite the days which already has been inputted into the date at the second step enumeration? Also I can't get the date code to compile properly, I just get the Date has no valid member called "add"</p>
  
  <pre><code>stepsQuery.initialResultsHandler = { query, results, error in
    let endDate = NSDate()
    let startDate = calendar.date(byAdding: .day, value: -365, to: endDate as Date, wrappingComponents: false)
    if let myResults = results{
      myResults.enumerateStatistics(from: startDate!, to: endDate as Date) { statistics, stop in
        if let quantity = statistics.sumQuantity(){
          var date = statistics.startDate
          let steps = quantity.doubleValue(for: HKUnit.count())
  
          var id = 0                  
          var dateToInsert = date
          var today = Date()
          var todaytwo = Date()
  
          for index in 0..&lt;stepy.step.count {
            if dateToInsert != today  {
              id = index + 1
              today.(Calendar.current.date(byAdding: .day, value: -1, to: today)
              stepy.step.append(0)                          
            }
            if date == dateToInsert as Date {                                    
              today.add(Calendar.current.date(byAdding: .day, value: -1, to: today)
              stepy.step.append(Int(steps))
              id = index + 1
            }
          }
        }
      }
    }
  }
  </code></pre>