找不到类型的初始值设定项

Cannot find an initializer for type

    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)

Cannot find an initializer for type 'HKQuantitySample' that accepts an argument list of type '(sampleType: HKQuantityType, predicate: NSPredicate, limit: Int, sortDescriptors: nil, resultsHandler: (_, _, _) -> _)'

只需将 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)
}

文档并没有像您提供的那样谈论任何初始化器……即使查看了 Beta 文档也没有找到任何关于您尝试调用的初始化器的信息。

请在此处查找现有 HKQuantitySample 个可用的初始值设定项:

https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKQuantitySample_Class/

有关创建查询的正确方法,请参阅 Dharmesh Kheni 的回答:)。