将swift 2.x转换为swift 3问题

da1*_*bi3 2 ios swift swift3

我已经使这个功能得到了每一天的所有步骤,但是我想将这个函数从swift 2.x转换为swift 3.但是我被困在两个不同的点上.

let anchorDate = calendar.dateComponents(anchorComponents)  
Run Code Online (Sandbox Code Playgroud)

我收到此错误:无法使用列表类型的参数(Datecomponents)调用dateCompontents


在这一点上:

let stepsQuery = HKStatisticsCollectionQuery(quantityType: HKQuantityType, quantitySamplePredicate: nil, options: .CumulativeSum, anchorDate: anchorComponents!, intervalComponents: interval)
Run Code Online (Sandbox Code Playgroud)

错误:无法将HKQuantityType.Type类型的值转换为预期的参数类型HKQuantityType.


完整的功能:

func getDailySteps(){
            let calendar = NSCalendar.current
            let interval = NSDateComponents()
            interval.day = 1

        var anchorComponents = calendar.dateComponents([.day , .month , .year], from: NSDate() as Date)
        anchorComponents.hour = 0
        let anchorDate = calendar.dateComponents(anchorComponents)

        // Define 1-day intervals starting from 0:00
        let stepsQuery = HKStatisticsCollectionQuery(quantityType: HKQuantityType, quantitySamplePredicate: nil, options: .CumulativeSum, anchorDate: anchorComponents!, intervalComponents: interval)

        // Set the results handler
        stepsQuery.initialResultsHandler = {query, results, error in
            let endDate = NSDate()
            let startDate = calendar.dateByAddingUnit(.Day, value: -7, toDate: endDate, options: [])
            if let myResults = results{                                                                             myResults.enumerateStatisticsFromDate(startDate!, toDate: endDate) { statistics, stop in
                if let quantity = statistics.sumQuantity(){
                    let date = statistics.startDate
                    let steps = quantity.doubleValueForUnit(HKUnit.countUnit())
                    print("\(date): steps = \(steps)")
                    //NOTE: If you are going to update the UI do it in the main thread
                    dispatch_async(dispatch_get_main_queue()){
                        //update UI components
                    }

                }
                } //end block
            } //end if let
        }

        HKHealthStore?.executeQuery(stepsQuery)

    }
Run Code Online (Sandbox Code Playgroud)

那么如何将此方法转换为swift 3?

Fel*_*SFD 8

您的代码包含的错误比您描述的多.我将你的代码复制到游乐场试图修复它.我不能保证它会起作用(我以前从未使用过HealthKit,但它至少应该编译.

错误0*

在Swift 3中,一些类已被重命名.例如NSCalendarCalendar.函数中的前两行必须更改为:

let calendar = Calendar.current
var interval = DateComponents()
Run Code Online (Sandbox Code Playgroud)

错误1

要从日期组件中获取日期,您必须使用此功能:

calendar.date(from: anchorComponents)
Run Code Online (Sandbox Code Playgroud)

错误2:

在阅读有关HKStatisticsCollectionQueryHKQuantityType的文档后,我想出了这行代码:

let stepsQuery = HKStatisticsCollectionQuery(quantityType: HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!, quantitySamplePredicate: nil, options: .cumulativeSum, anchorDate: anchorDate!, intervalComponents: interval)
Run Code Online (Sandbox Code Playgroud)

HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)替换HKQuantityType旧代码中的内容.

anchorDate:收到Date而不是DateComponents.

你传递的枚举案例options:以Swift 3中的小写字母开头.

错误3*

dateByAddingUnit已经变了.在Swift 3中,你使用这个:

calendar.date(byAdding: .day, value: -7, to: endDate)
Run Code Online (Sandbox Code Playgroud)

(同样,enum-case现在以小写字母开头.)

错误4*

enumerateStatistics也改变了.下雪了:

enumerateStatistics(from: startDate!, to: endDate)
Run Code Online (Sandbox Code Playgroud)

错误5*

HKUnit.countUnit() 已重命名:

let steps = quantity.doubleValue(for: HKUnit.count())
Run Code Online (Sandbox Code Playgroud)

错误6*

使用多线程的方式已经大量改变.

DispatchQueue.main.async {
    //update UI components
}
Run Code Online (Sandbox Code Playgroud)

错误7*

HKHealthStore?.executeQuery(stepsQuery)有点奇怪.execute(_:)(因为它在Swift 3中命名)不是静态的,只能用于实例HKHealthStore.我用以下行修复了错误消息,但我不确定,这是否真的有效.

HKHealthStore().execute(stepsQuery)
Run Code Online (Sandbox Code Playgroud)

*在Playground中使用时,我在代码中发现的其他错误.