title: How can daily steps total be queried from Healthkit and displayed into SwiftUI text view?
body: <p>I am trying to get the total number of steps that a user has taken today and display this number of steps into a SwiftUI text view on an Apple Watch screen.</p>
  
  <p>All the code snippets that I'm finding online aren't working. It seems like hkstatisticscollectionquery should be what is used to get this type of data, but the code in Apple documentation isn't working for me. I have already asked the user for permission to read steps data from healthkit so all that needs to be done is to query this data and put into a SwiftUI text view. Below is Apple's developer documentation that contains the code that I can't get to work.</p>
  
  <p><a href="https://developer.apple.com/documentation/healthkit/hkstatisticscollectionquery" rel="nofollow noreferrer">https://developer.apple.com/documentation/healthkit/hkstatisticscollectionquery</a></p>
  
  <p>Here is the code that Apple supplies to query steps that is not working for me.</p>
  
  <pre class="lang-swift prettyprint-override"><code>let calendar = NSCalendar.currentCalendar()
  
  let interval = NSDateComponents()
  interval.day = 7
  
  // Set the anchor date to Monday at 3:00 a.m.
  let anchorComponents = calendar.components([.Day, .Month, .Year, .Weekday], fromDate: NSDate())
  
  
  let offset = (7 + anchorComponents.weekday - 2) % 7
  anchorComponents.day -= offset
  anchorComponents.hour = 3
  
  guard let anchorDate = calendar.dateFromComponents(anchorComponents) else {
      fatalError("*** unable to create a valid date from the given components ***")
  }
  
  guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) else {
      fatalError("*** Unable to create a step count type ***")
  }
  
  // Create the query
  let query = HKStatisticsCollectionQuery(quantityType: quantityType,
                                          quantitySamplePredicate: nil,
                                          options: .CumulativeSum,
                                          anchorDate: anchorDate,
                                          intervalComponents: interval)
  
  // Set the results handler
  query.initialResultsHandler = {
      query, results, error in
  
      guard let statsCollection = results else {
          // Perform proper error handling here
          fatalError("*** An error occurred while calculating the statistics: \(error?.localizedDescription) ***")
      }
  
      let endDate = NSDate()
  
      guard let startDate = calendar.dateByAddingUnit(.Month, value: -3, toDate: endDate, options: []) else {
          fatalError("*** Unable to calculate the start date ***")
      }
  
      // Plot the weekly step counts over the past 3 months
      statsCollection.enumerateStatisticsFromDate(startDate, toDate: endDate) { [unowned self] statistics, stop in
  
          if let quantity = statistics.sumQuantity() {
              let date = statistics.startDate
              let value = quantity.doubleValueForUnit(HKUnit.countUnit())
  
              // Call a custom method to plot each data point.
              self.plotWeeklyStepCount(value, forDate: date)
          }
      }
  }
  
  healthStore.executeQuery(query)
  </code></pre>
  
  <p>How can the total number of steps that a user has taken today be queried from Healthkit and displayed into a SwiftUI text view?</p>