         
         healthStore.executeQuery(sampleQuery)
     }
-  
-  
-  private func dataTypesToRead() -> NSSet {
-    
-    var typeIds = [HKQuantityTypeIdentifierHeight, HKQuantityTypeIdentifierBodyMass, HKQuantityTypeIdentifierDistanceWalkingRunning]
-    
-    
-    var typeBodyMass = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)
-    //    var type2 = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)
-    //    var type3 = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
-    var typeStepCount = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
-    
-    return NSSet(objects: typeStepCount, typeBodyMass)
-    
-  }
+    
+    func saveUserWeight(weight:Double) {
+        let bmType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)
+        let bmQuantity = HKQuantity(unit: HKUnit.gramUnit(), doubleValue:  weight * 1000 )
+        let sample = HKQuantitySample(type: bmType, quantity: bmQuantity, startDate: NSDate(), endDate: NSDate())
+        
+        
+        self.healthStore.saveObject(sample, withCompletion: {
+            (success, error) in
+            if !success {
+                let alertController = UIAlertController(title: "Error", message: "Error saving data on HealthKit", preferredStyle: .Alert)
+            } else {
+                self.userWeight = Int(weight)
+            }
+        })
+    }
+    
+    func setWeightInUI(weight:Int, animation: Bool = false) {
+        self.userWeight = weight
+        self.weightPicker.selectRow(self.selectedRowInComponent(weight), inComponent: 0, animated: animation)
+    }
+  
+  
+    private func dataTypesToRead() -> NSSet {
+        var typeIds = [HKQuantityTypeIdentifierHeight, HKQuantityTypeIdentifierBodyMass, HKQuantityTypeIdentifierDistanceWalkingRunning]
+        var typeBodyMass = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)
+        //    var type2 = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)
+        //    var type3 = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
+        var typeStepCount = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
+        
+        return NSSet(objects: typeStepCount, typeBodyMass)
+        
+    }
+    
+    private func dataTypesToWrite() -> NSSet {
+        var typeBodyMass = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)
+        
+        return NSSet(objects: typeBodyMass)
+        
+    }
 }
 

    
    // weightPicker configuration
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
        return 1
    }

//        self.selectUserWeight()
        self.stepCounter = StepCounter(healthStore: healthStore, delegate: self);
      
        healthStore.requestAuthorizationToShareTypes( dataTypesToWrite(), readTypes: dataTypesToRead(), completion: {
        (success:Bool, error:NSError!) -> Void in
        //      if !success{
        //        var alert = UIAlertView(title: "Missed authorization", message: "The app cannot work without authorization for HealthKit. Closing", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "Close")
