Swift2-香港样本查询


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
})

在闭包中,resultserror是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
}

相关内容

  • 没有找到相关文章

最新更新