Search code examples
iosswiftcore-bluetoothcbperipheralmanager

init with delegate: Type of expression is ambiguous without more context


Feeling pretty stupid because I had a very similar question not too long ago:

init CBCentralManager: Type of expression is ambiguous without more context

This time I'm instantiating a CBPeripheralManager inside viewDidLoad to an iVar (attempted both 'lazy' and '!' modifiers).

I've conformed to the delegate (both on type and as an extension), but I still get the same error when I try to assign self instead of nil for the delegate parameter.

class vc: UIViewController {
    var peripheralManager: CBPeripheralManager!

    override func viewDidLoad() {
        super.viewDidLoad()            
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
    }
}

extension vc: CBPeripheralDelegate { }

How do I get self as an instance here, or whatever is the actual issue?


Solution

  • Your extension is conforming to the wrong protocol.

    Change:

    extension vc: CBPeripheralDelegate
    

    to:

    extension vc: CBPeripheralManagerDelegate
    

    And then you must implement at least the minimum required protocol methods.