user5087998
user5087998

Reputation:

Trying to save metadata - HealthKit

I'm trying to save some data to HealthKit. Sending a UUID with each item. It's a NSUUID converted into a string.

hk_acceptsMetadataValue:]: unrecognized selector sent to

I don't get what I'm doing wrong. Any ideas?

    // Save new item to the Health App
    func saveSample(amount:Double, date:NSDate, uuid: String ) {

        // Create metadata
        let metadata : NSDictionary = [HKMetadataKeyExternalUUID : uuid]

        // Create a Sample
        let unit = HKUnit.literUnitWithMetricPrefix(.Milli)
        let type = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryWater)
        let quantity = HKQuantity(unit: unit, doubleValue: amount)
        let sample = HKQuantitySample(type: type!, quantity: quantity,
 startDate: date, endDate: date, metadata:metadata as! [String : AnyObject])

        // Save the Sample in the store
        healthKitStore.saveObject(sample, withCompletion: { (success, error) -> Void in
            if( error != nil ) {
                print("Error saving Sample: \(error!.localizedDescription)")
            } else {
                print("Sample saved successfully!")
            }
        })
    }

Because metadata should be a string Xcode suggested I add as! [String : AnyObject] after the variable metadata when I create the sample.

Still got the same error

Upvotes: 1

Views: 971

Answers (1)

Allan
Allan

Reputation: 7353

You are encountering a known issue with HealthKit. You can work around the bug by explicitly creating an NSDictionary for your metadata instead of a Swift dictionary.

Upvotes: 1

Related Questions