ios - 尝试保存元数据 - HealthKit

标签 ios swift healthkit hkhealthstore

我正在尝试将一些数据保存到 HealthKit。为每个项目发送一个 UUID。它是一个转换成字符串的 NSUUID。

hk_acceptsMetadataValue:]: unrecognized selector sent to

我不明白我做错了什么。有什么想法吗?

    // 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!")
            }
        })
    }

因为元数据应该是一个字符串 Xcode 建议我添加为! [String : AnyObject] 在我创建示例时在变量元数据之后。

还是一样的错误

最佳答案

您在使用 HealthKit 时遇到了一个已知问题。您可以通过为元数据显式创建 NSDictionary 而不是 Swift 字典来解决该错误。

关于ios - 尝试保存元数据 - HealthKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262108/

相关文章:

ios - 如何在键盘选择菜单中编辑自定义键盘名称

ios - 将 NSManagedObject 子类核心数据对象打印到控制台在 Swift 中返回空行

javascript - _rnAppleHealthKit.default.initHealthKit 不是函数 -> rn-apple-healthkit

ios - Apple Health 应用程序的哪些部分可通过 x-apple-health ://URL scheme? 访问

ios - Swift 如何从 View Controller 运行函数并更改 SKScene 中的变量?

ios - 在 iOS 模拟器中使用 MPMediaPickerController 时出现运行时错误

ios - 为什么 Social.localUser.Authenticate 在 Unity 应用程序没有互联网连接时会导致崩溃?

xcode - 从新的 Firebase 中检索数据

xcode - 将图像路径存储在plist中然后检索以在屏幕上显示

SWIFT:CDA 到 iOS 10 Health 应用程序