Sunil Kumar
Sunil Kumar

Reputation: 1105

Class does not confirm to Protocol CBPeripheralManagerDelegate

I am new to IOS swift development, I am implementing a class PeripheralHandler in which i am initialising an instance of CBPeripheralManager. I have implemented the required delegate methods but still it showing error 'Type PeripheralHandler ->() -> PeripheralHandler!' does not confirm to CBPeripheralManagerDelegate.

import Foundation
import CoreBluetooth

class PeripheralHandler : NSObject, CBPeripheralManagerDelegate{

    var myPeripheralManager = CBPeripheralManager(delegate:self, queue: nil)

    func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!)
    {
        println("peripheralManagerDidUpdateState called !!!")

        switch peripheral.state
        {
        case CBPeripheralManagerState.PoweredOff:
            println("BLE OFF")
        case CBPeripheralManagerState.PoweredOn:
            println("BLE ON")
        case CBPeripheralManagerState.Unknown:
            println("NOT RECOGNIZED")
        case CBPeripheralManagerState.Unsupported:
            println("BLE NOT SUPPORTED")
        case CBPeripheralManagerState.Resetting:
            println("BLE NOT SUPPORTED")
        default:
            println("Error")
        }
    }
}

Upvotes: 0

Views: 725

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

That's a misleading error message. You can't reference self in the initial assignment of the instance variable. When instance variables are instantiated self doesn't necessarily contain anything meaningful, so Swift doesn't allow to use self.

You could use a lazy variable:

lazy var myPeripheralManager: CBPeripheralManager = {
    return CBPeripheralManager(delegate:self, queue: nil)
}()

This block will be called when you first access myPeripheralManager, and it will create the object for you.

Upvotes: 2

Related Questions