Roo
Roo

Reputation: 279

Swift, multiple inheritance from classes

Here is my custom class that I have created:

import UIKit
import CoreBluetooth

protocol vaBeanDelegate
{

}

class vaBean: CBCentralManager, CBCentralManagerDelegate {

override init!(delegate: CBCentralManagerDelegate!, queue: dispatch_queue_t!) {
    println("bean initialised")
    super.init()
    self.delegate = delegate
}

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
    println("discovered peripheral(s)")
}

func centralManagerDidUpdateState(central: CBCentralManager!) {
    switch (central.state) {
    case .PoweredOff:
        println("hardware is powered off")

    case .PoweredOn:
        println("hardware is powered on and ready")
        //centralManager.scanForPeripheralsWithServices(nil, options: nil)
    case .Resetting:
        println("hardware is resetting")

    case .Unauthorized:
        println("state is unauthorized")

    case .Unknown:
        println("state is unknown");

    case .Unsupported:
        println("hardware is unsupported on this platform");

    }
}

func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
    println("didConnectPeripheral")
}
}

I just don't know how to initialize it from my main ViewController. When I try the following it complains that ViewController does not conform to the CBCentralManagerDelegate:

import UIKit

class ViewController: UIViewController {

var myBean: vaBean!

override func viewDidLoad() {
    super.viewDidLoad()
    println("blueToothTest v1.00")
    println("opening bluetooth connection ...")
    myBean = vaBean(delegate: self, queue: nil)
}

override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}
}

Upvotes: 11

Views: 23685

Answers (4)

Robin Bork
Robin Bork

Reputation: 327

Your ViewController is not conforming to the protocol CBCentralManagerDelegate

Looking at this:

myBean = vaBean(delegate: self, queue: nil)

The first argument of the constructor init expects an object for the parameter delegate that conforms to the protocol CBCentralManagerDelegate. You pass self there, which is your view controller, which does not implement that delegate (even though it does not define any functions yet. That will throw you the error.

You have 2 choices here to solve this:

1) Implement CBCentralManagerDelegate directly in your view controller by declaring it this way:

class ViewController: UIViewController, CBCentralManagerDelegate { 
    // Here goes the rest of your class
    // Don't forget to add delegate functions once you added some
}

2) Add an extension to the class at the end of your file that adds conformance for the protocol

extension UIViewController: CBCentralManagerDelegate { 
    // Empty for now since the protocol does not require anything at the moment
    // Don't forget to add delegate functions once you added some
}

Upvotes: 1

Eddie.Dou
Eddie.Dou

Reputation: 469

If it complains that ViewController does not conform to the CBCentralManagerDelegate, you can try to use below code.

extension viewController: CBCentralManagerDelegate {
    //implement the methods in CBCentralManagerDelegate here
}

In swift that is single inheritance only as Abizern said above, but that support implements multiple Protocols, like Interface in Java.

In your sample code, vaBean implement protocol CBCentralManagerDelegate.

Upvotes: 0

Abizern
Abizern

Reputation: 150595

Swift and Objective-C are single inheritance only, you can't have more than one superclass for a class.

When you see Swift code with what looks like multiple superclasses in their declaration, you'll see that at most, one of the names in the declaration is a superclass, the others are names of protocols.

In Objective-C the these would have been written with <angled brackets> but this is not the case for Swift.

Upvotes: 18

Todd Agulnick
Todd Agulnick

Reputation: 1985

Swift doesn't support multiple inheritance. But your ViewController doesn't need to inherit from vaBeanDelegate if you merely want to access it, as your question suggests.

Upvotes: 2

Related Questions