Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Swift从HealthKit读取日期间隔的步骤_Ios_Swift_Nsdate_Healthkit_Hksamplequery - Fatal编程技术网

Ios 使用Swift从HealthKit读取日期间隔的步骤

Ios 使用Swift从HealthKit读取日期间隔的步骤,ios,swift,nsdate,healthkit,hksamplequery,Ios,Swift,Nsdate,Healthkit,Hksamplequery,我今天有这个方法来阅读HealthKit的步骤 func todaySteps(completion: (Double, NSError?) -> () ) { // this function gives you all of the steps the user has taken since the beginning of the current day. let type = HKSampleType.quantityTypeForIdentifier(HKQuanti

我今天有这个方法来阅读HealthKit的步骤

func todaySteps(completion: (Double, NSError?) -> () )
{ // this function gives you all of the steps the user has taken since the beginning of the current day.

    let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) // The type of data we are requesting


    let date = NSDate()
    print(date)
    let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let newDate = cal.startOfDayForDate(date)
    print(newDate)
    let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None) // Our search predicate which will fetch all steps taken today

    // The actual HealthKit Query which will fetch all of the steps and add them up for us.
    let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in
        var steps: Double = 0

        if results?.count > 0
        {
            for result in results as! [HKQuantitySample]
            {
                steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
            }
        }

        completion(steps, error)
    }

    executeQuery(query)
}
现在,假设我想阅读2016年6月1日至2016年6月7日期间每天的总步骤


我该怎么做呢?请指导我谢谢

使用此功能可以获得多个日期的步骤

func MultipleDaysStepsAndActivities(_ startDate:Date, completion: @escaping (NSDictionary, [HealthKitManuallActivity], NSError?) -> () ) {

    let calender = NSCalendar.current
    let now = Date()
    _ = calender.component(.year, from: now)

    let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount) // The type of data we are requesting
    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: now, options: HKQueryOptions())

    var interval = DateComponents()
    interval.day = 1

    let query = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: startDate, intervalComponents:interval)

    query.initialResultsHandler = { query, results, error in

        if error != nil {
            print(error as Any)
            return
        }

        var dict:[String:Double] = [:]

        if let myResults = results {
            myResults.enumerateStatistics(from: startDate, to: now) {
                statistics, stop in

                if let quantity = statistics.sumQuantity() {

                    let steps = quantity.doubleValue(for: HKUnit.count())
                    print("Steps = \(steps.rounded())")
                    dict[self.fmt.string(from: statistics.startDate)] = steps.rounded()
                }
            }
        }

    }

    execute(query)

}