【问题标题】:Swift ambiguous use of UUID with CoreBluetooth and HealthKitSwift 模糊使用 UUID 与 CoreBluetooth 和 HealthKit
【发布时间】:2015-06-01 15:17:01
【问题描述】:

我正在创建一个需要访问 CoreBluetooth 才能与心率监测器交互的 iOS Swift 应用。这工作正常。

我还想使用 HealthKit 保存心率,但是当我将 HealthKit 导入应用程序时(即使在单独的文件中),我收到以下错误;

这与 service.UUID 有关。虽然这里只打印该行,但 service.UUID 用于其他位置并给出相同的错误。但是,如果没有 HealthKit 导入,service.UUID 可以正常工作。

func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
    for service in peripheral.services{
        NSLog("Discovered service: \(service.UUID)")
        peripheral.discoverCharacteristics(nil, forService: service as CBService)
    }
}

我已经分别使用了 HealthKit 和 CoreBluetooth,它工作正常,但是将两者结合起来会产生歧义错误。有没有办法明确告诉它应该使用哪个?

谢谢,

【问题讨论】:

  • 试试这个for service in peripheral.services as [CBService]{}
  • @MidhunMP 完美!解决了这个问题,谢谢。您想将其添加为 Q,我将其标记为答案。

标签: ios swift uuid core-bluetooth healthkit


【解决方案1】:

问题在于 CBService 和 HealthKit 都具有 UUID 属性。当您调用 peripheral.services 时,它会返回一个 AnyObject 数组。所以修复了这个问题,将其转换为CBService。

for service in peripheral.services as [CBService]
{
}

【讨论】:

    【解决方案2】:

    HealthKit 在您创建对象时为其分配一个 UUID。如果您想添加自己的唯一 ID,请使用 HKMetadataKeyExternalUUID 键将其添加到对象的元数据中。

    来源:https://developer.apple.com/library/prerelease/ios/documentation/HealthKit/Reference/HKObject_Class/index.html#//apple_ref/occ/instp/HKObject/UUID

    【讨论】:

    • 不是我想为 HealthKit 分配一个 uniqueID,而是当我导入 HealthKit 时,代码不知道应该使用 CoreBluetooth UUID 还是 HealthKit UUID。
    • 您必须将 CoreBluetooth 的 UUID 添加到对象的元数据中。
    猜你喜欢
    • 2017-12-09
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多