0
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)'

正确的语法将如何?

4

1 回答 1

0

我不是 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

}
于 2015-06-27T21:01:41.817 回答