从函数返回的值是空的:为什么? [英] Return values from function is empty: Why?
问题描述
在QueryHK类中,我运行HealthKit查询以获取步骤和相应的日期.我想将这些数据写入NSArray并返回它,以便可以在ViewController中调用该函数.
In QueryHK class I run a HealthKit query for steps and corresponding date. I would like to write those data to an NSArray and return it so that I can call the function in the ViewController.
- 问题:ViewController中的代码不返回任何内容,也没有错误.为什么退货是空的?
- Question: the code from ViewController returns nothing, and no error. Why is the return empty?
在我看来,查询没有写入返回值".
It seems to me that the query does not "write to the return".
QueryHK.swift:
import UIKit
import HealthKit
class QueryHK: NSObject {
var steps = Double()
var date = NSDate()
func performHKQuery () -> (steps: Double, date: NSDate){
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
for sample in samples as [HKQuantitySample]
{
self.steps = sample.quantity.doubleValueForUnit(stepsUnit)
self.date = sample.startDate
println("Query HealthKit steps: \(self.steps)")
println("Query HealthKit date: \(self.date)")
}
}
healthKitManager.healthStore.executeQuery(sampleQuery)
return (steps, date)
}
}
ViewController.swift:
import UIKit
class ViewController: UIViewController {
var query = QueryHK()
override func viewDidLoad() {
super.viewDidLoad()
printStepsAndDate()
}
func printStepsAndDate() {
println(query.performHKQuery().date)
println(query.performHKQuery().steps)
}
}
推荐答案
原因是HKSampleQuery
是异步处理的-它立即返回并在后台运行.因此,您的方法立即完成执行,而不是在结果处理程序块中处理响应.您将需要更新方法以采用完成块而不是返回值.
The reason is that HKSampleQuery
is processed asynchronously - it returns right away and does it's work in the background. So your method finishes executing right away instead of handling the response in the results handler block. You would need to update your method to take a completion block instead of returning values.
QueryHK.swift:
import UIKit
import HealthKit
struct Sample {
let step: Double
let date: NSDate
}
class QueryHK: NSObject {
func performHKQuery(completion: (samples: [Sample]) -> 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
var processedSamples = [Sample]()
for sample in samples as [HKQuantitySample]
{
processedSamples.append(Sample(step: sample.quantity.doubleValueForUnit(stepsUnit), date: sample.startDate))
println("Query HealthKit steps: \(processedSamples.last?.step)")
println("Query HealthKit date: \(processedSamples.last?.date)")
}
// Call the completion handler with the results here
completion(samples: processedSamples)
}
healthKitManager.healthStore.executeQuery(sampleQuery)
}
}
ViewController.swift:
import UIKit
class ViewController: UIViewController {
var query = QueryHK()
override func viewDidLoad() {
super.viewDidLoad()
printStepsAndDate()
}
func printStepsAndDate() {
query.performHKQuery() { steps in
println(steps)
}
}
}
这篇关于从函数返回的值是空的:为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!