let endDate = NSDate()
let startDate = NSDate()
let v : Float?
let stepsCount:HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!
let predicate:NSPredicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
let query = HKQuantitySample(sampleType: stepsCount, predicate: predicate, limit: 1, sortDescriptors: nil, resultsHandler: {
(query, results, error) in
if results == nil {
print(error)
}
v = result.first.Quantity
})
healthStore.executeQuery(query)
找不到类型' HKQuantitySample'的初始化程序。接受 类型'的参数列表(sampleType:HKQuantityType,谓词: NSPredicate,limit:Int,sortDescriptors:nil,resultsHandler:(_,_, _) - > _)'
答案 0 :(得分:2)
只需将HKQuantitySample
替换为HKSampleQuery
即可。
有关更多信息,请参阅THIS教程。
您可以在这里找到示例代码:
func readMostRecentSample(sampleType:HKSampleType , completion: ((HKSample!, NSError!) -> Void)!)
{
// 1. Build the Predicate
let past = NSDate.distantPast() as! NSDate
let now = NSDate()
let mostRecentPredicate = HKQuery.predicateForSamplesWithStartDate(past, endDate:now, options: .None)
// 2. Build the sort descriptor to return the samples in descending order
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)
// 3. we want to limit the number of samples returned by the query to just 1 (the most recent)
let limit = 1
// 4. Build samples query
let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: limit, sortDescriptors: [sortDescriptor])
{ (sampleQuery, results, error ) -> Void in
if let queryError = error {
completion(nil,error)
return;
}
// Get the first sample
let mostRecentSample = results.first as? HKQuantitySample
// Execute the completion closure
if completion != nil {
completion(mostRecentSample,nil)
}
}
// 5. Execute the Query
self.healthKitStore.executeQuery(sampleQuery)
}
答案 1 :(得分:0)
文档没有讨论任何初始化程序,例如您提供的初始化程序...甚至查看了Beta文档,但没有发现任何关于您要调用的文档。
请在此处查看现有的HKQuantitySample
初始化程序:
https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKQuantitySample_Class/
请参阅Dharmesh Kheni关于创建查询的正确方法的答案:)。