CBPeripheral是一个痛苦的测试对象,因为它本身无法实例化。因此,我在它周围使用了包装器(HBRPeripheralWrapper)(也用于其他目的)。

我想在包装器上转发大部分通话
HBRPeripheralWrapper)到实际的包装对象CBPeripheral

从技术上讲,它使用forwardInvocation起作用,但是如何在Swift中采用类似的模式?

PS:NSInvocationSwift中不可用

class HBRPeripheralWrapper {
  let peripheral:CBPeripheral

  // I would like to avoid this "manual" forwarding
  var identifier: NSUUID {
      get {
          return peripheral.identifier
      }
  }

  init(peripheral:CBPeripheral) {
    self.peripheral = peripheral
  }

  // target forwarding is great, but how can I make the compiler happy?
  override func forwardingTargetForSelector(aSelector: Selector) -> AnyObject? {
    if(self.peripheral.respondsToSelector(aSelector)) {
      return self.peripheral
    }
    return super.forwardingTargetForSelector(aSelector)
  }

}

最佳答案

代替扩展HBRPeripheralWrapper,考虑扩展CBPeripheral

extension CBPeripheral {
    // add whatever else you need here.
}


除了转发外,您没有说完全在HBRPeripheralWrapper中做什么,所以我无法提供更多帮助。

10-04 15:35