Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()}
}
like image 583
Roo Avatar asked Oct 30 '14 23:10

Roo


People also ask

Can you inherit from multiple classes?

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B's constructor is called before A's constructor.

Which kind of inheritance does Swift support for classes?

Yes in Swift and Objective-c Single and Multilevel inheritance is supported. In swift and many other languages Multiple Inheritance is restricted by use of classes because of historical problems like deadly diamond and others ambiguities.In swift you can achieve the Multiple inheritance at some level by Protocols .

Is inheritance possible in Swift?

Like other OOP languages, Swift also supports the concept of class inheritance. Inheritance allows us to create a new class from an existing class.


1 Answers

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.

like image 110
Abizern Avatar answered Sep 23 '22 15:09

Abizern