ios – Swift,来自类的多重继承

前端之家收集整理的这篇文章主要介绍了ios – Swift,来自类的多重继承前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我创建的自定义类:
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")
}
}

我只是不知道如何从我的主ViewController初始化它.当我尝试以下操作时,它会抱怨ViewController不符合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()}
}

解决方法

Swift和Objective-C只是单一继承,一个类不能有多个超类.

当您在声明中看到Swift代码看起来像多个超类时,您会看到最多,声明中的一个名称是超类,其他名称是协议名称.

在Objective-C中,这些将用<有角度的括号>写成.但Swift的情况并非如此.

原文链接:https://www.f2er.com/iOS/334655.html

猜你在找的iOS相关文章