1

これが問題です。今日の手順を、completionHandler の Core Data に保存したいと考えています。しかし、この値をロードしている間、コンパイラはそれを nil と表示します。解決策はありますか?

 func fetchDataOfQuantityType(startDate: NSDate, endDate: NSDate, quantityType: HKQuantityType, completion:((NSArray, NSError!) -> Void)!)  {

    // initial a predicate with startDate and endDate
    let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: HKQueryOptions.StrictStartDate)

    // initialize a HKStatisticsQuery
    let query: HKStatisticsQuery = HKStatisticsQuery(quantityType: quantityType, quantitySamplePredicate: predicate, options: HKStatisticsOptions.CumulativeSum, completionHandler: { (query, results, error) in

        if error != nil { // if there is an error print it
            println("there is a \(error) occur")
            return
        }
        // Mark: - Saving the data in to Core data

        var todaySteps = results.sumQuantity().doubleValueForUnit(HKUnit.countUnit())
        println("The totalstep for today is \(todaySteps)")
            var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
            var context: NSManagedObjectContext = appDel.managedObjectContext!
            var newUser = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: context) as NSManagedObject
            newUser.setValue(todaySteps, forKey: "todaysteps")
            })
    self.healthstore.executeQuery(query)
}
4

1 に答える 1