Ismail Bannan
Ismail Bannan

Reputation: 13

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: (_, _, _) -> _)'

Upvotes: 1

Views: 156

Answers (2)

Fantattitude
Fantattitude

Reputation: 1842

Documentation doesn't talk about any initializer like the one you are providing… Even looked in the Beta docs and didn't found anything about the one you are trying to call.

Please look here for existing HKQuantitySample initializers available :

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

See Dharmesh Kheni's answer for the correct way to create a query :).

Upvotes: 0

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

Just replace HKQuantitySample with HKSampleQuery and it will work fine.

For more Info refer THIS tutorial.

Where you can find sample code like:

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

Upvotes: 2

Related Questions