我想在CBPeripheralDelegate中定义一个选择器,func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)
。
在swift3中,它重命名为peripheral(_: didUpdateValueFor:error)
,与func peripheral(peripheral: CBPeripheral, didUpdateValueForDescriptor descriptor: CBDescriptor, error: NSError?)
因此,当我尝试定义这样的选择器#selector(CBPeripheralDelegate.peripheral(_:didUpdateValueFor:error:))
时会导致编译错误:ambiguous use
。
我尝试定义doc描述:#selector(((CBPeripheralDelegate.peripheral(_:didUpdateValueFor:error:)) as (CBPeripheralDelegate) -> (CBPeripheral, CBCharacteristic, NSError) -> Void)
,
失败了。
那么在swift3中定义选择器的正确方法是什么?
答案 0 :(得分:1)
我担心这可能是当前height
表示法中的一个缺陷,您应该向Apple或swift.org发送错误报告。 (或者我可能只是遗漏了一些东西......)
要解决此问题,请定义符合协议的类,并定义要为其创建选择器的方法。
#selector
在class TheClass: NSObject, CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
//No need to actually implement the method
}
}
中使用该类名:
#selector()
您可能已经有一个实现该方法的类,然后您可以使用它的类名。
Objective-C选择器不保留类名信息,因此,选择器可用于任何可能实现该方法的类。
如果要从符合协议的类中创建选择器并具有该方法的定义,可以将其写为:
#selector(TheClass.peripheral(_:didUpdateValueFor:error:))