1

Bluetooth LE、Bluetooth などのデバイスを補うために従来のポリモーフィズムを使用するデリゲートを作成しようとしていますが、キャストするための正しい構文を取得できないようです。

ここに私の親プロトコルとクラスがあります:

@objc protocol DeviceDelegate
{
    func didConnectToDevice(name:String)
    func didFailToConnectToDevice(name:String)
    func didDisconnectFromDevice(name:String)

    func didWriteData(data:NSData)
    func didReceiveData(data:NSData)
}

class Device: NSObject
{
    var delegate: DeviceDelegate?
}

ここで、子クラスとプロトコルを簡略化して示します。

protocol BluetoothDelegate : DeviceDelegate
{
    func didFindService(name:String)
    func didFindCharacteristic(name:String)
}

class BLE: Device
{
    func someFunc()
    {
        let bluetoothDelegate = (delegate as? BluetoothDelegate)
        bluetoothDelegate.didFindService(UUIDString)
    }
}

その関数の最初の行で次のエラーがスローされます。

Cannot downcast from 'DeviceDelegate?' to non-@objc protocol type 'BluetoothDelegate'

通常のオブジェクトのように子へのキャストを許可する必要があるため、これは私には意味がありません。

@objc を BluetoothDelegate の前に置くと、次のエラーが発生します。

@objc protocol 'BluetoothDelegate' cannot refine non-@objc protocol 'DeviceDelegate'

誰でもこれについて何か考えがありますか?

4

1 に答える 1