let hkSampleType:HKSampleType = HKSampleType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierFood)!
let query = HKSampleQuery(sampleType: hkSampleType, predicate: predicate, limit: 80, sortDescriptors: nil, resultsHandler: { (query:HKSampleQuery, results:[HKSample]!, error:NSError!) -> Void in
//code
})
就是这样,但我收到一个错误:Cannot find an initializer for type 'HKSampleQuery' that accepts an argument list of type '(sampleType: HKSampleType, predicate: NSPredicate, limit: Int, sortDescriptors: nil, resultsHandler: (HKSampleQuery, [HKSample]!, NSError!) -> Void)'
正确的语法是什么?
我不是Swift或HealthKit的专家,但这是在Xcode 7:中编译的
let hkSampleType:HKSampleType = HKSampleType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierFood)!
let query = HKSampleQuery(sampleType: hkSampleType, predicate: nil, limit: 80, sortDescriptors: nil, resultsHandler: { (query:HKSampleQuery, results:[HKSample]?, error:NSError?) -> Void in
//code
})
在闭包中,results
和error
是iOS 9中的可选项。你宣布它们已打开。
有一件事我不确定,你能用HKSampleQuery
代替HKCorrelation
吗。以下是HKCorrelationQuery
的正确声明,以备您需要:
let corralationType = HKCorrelationType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierFood)!
let query = HKCorrelationQuery(type: corralationType, predicate: nil, samplePredicates: nil) { (query: HKCorrelationQuery, results: [HKCorrelation]?, error: NSError?) -> Void in
}