我如何将结果处理程序的结果赋值给一个variables?

我试图从完成处理程序中分配结果,以便我可以使用它在图库中进行绘图。 目前数据以控制台输出的forms输出到控制台

[(200.0,2014-12-26 12:44:00 +0000),300.0,2014-12-25 12:44:00 +0000)]

– 问题:我需要在一个情节中使用这个数据。 如何在ViewController或QueryHKArray2中更改我的代码,以便可以将数据分配给与此类似的variables:

  • var dateArray = query.performHKQuery()。date
  • var stepArray = query.performHKQuery()。stepCount

任何帮助深表感谢 !

视图控制器:

import UIKit class ViewControllerArray2: UIViewController { var query = QueryHKArray2() override func viewDidLoad() { super.viewDidLoad() printStepsAndDate() } func printStepsAndDate() { query.performHKQuery() { stepCount in println(stepCount) } } 

QueryHKArray2类:

 import UIKit import HealthKit class QueryHKArray2: NSObject { func performHKQuery(completion: ([(steps: Double, date: NSDate)]) -> Void) { let healthKitManager = HealthKitManager.sharedInstance let stepsSample = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) let stepsUnit = HKUnit.countUnit() let sampleQuery = HKSampleQuery( sampleType: stepsSample, predicate: nil, limit: 0, sortDescriptors: nil) { (sampleQuery, samples, error) in if let samples = samples as? [HKQuantitySample] { let steps = samples.map { (sample: HKQuantitySample)->(steps: Double, date: NSDate) in let stepCount = sample.quantity.doubleValueForUnit(stepsUnit) let date = sample.startDate return (steps: stepCount, date: date) } completion(steps) } } healthKitManager.healthStore.executeQuery(sampleQuery) } } 

您可以循环访问stepCount并获取tuple

 func printStepsAndDate() { var dateArray:[NSDate] = [] var stepArray:[Double]= [] query.performHKQuery() { stepCount in for temp in stepCount { stepArray.append(temp.steps) dateArray.append(temp.date) } } }