Delegation in NSObject Classes

2019-09-14 22:03发布

问题:

I have used delegates in Viewcontrollers and they worked well but now I want the delegation process between my two NSObject classes. But getting crash : fatal error: unexpectedly found nil while unwrapping an Optional value

Here is what I am doing :

Basic Device Class :

class basicDevice : NSObject, CBPeripheralDelegate  {
    public static let NOTIFICATION_SERVICES_DISCOVERED = Notification.Name(rawValue: "NOTIFICATION_SERVICES_DISCOVERED")

    private var listOfServices:[String:[String]] = [:]
    private var bleDevice:CBPeripheral?

    func setPheripheral(device:CBPeripheral) {
        bleDevice = device;
        bleDevice!.delegate = self;
    }

    func getPheripheral() -> CBPeripheral? {
        return bleDevice;
    }

    func getListOfServices() -> [String:[String]] {
        return listOfServices
    }

    func onConnected() {
        bleDevice!.delegate = self
        self.bleDevice!.discoverServices(nil)
    }

    func onDisconnection() {
        bleDevice!.delegate = nil
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        if error != nil { print(" didDiscoverServices:: \(error!)") }
        for service in peripheral.services!
        {
            listOfServices[service.uuid.uuidString] = []
            print("Discovered service: \(service.uuid)")
            peripheral.discoverCharacteristics(nil, for: service )
        }

        OperationQueue.main.addOperation({
            NotificationCenter.default.post(name: basicDevice.NOTIFICATION_SERVICES_DISCOVERED, object: nil)
        })
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        if error != nil { print(" didDiscoverCharacteristicsFor:: \(error!)") }

        for characteristic in service.characteristics as [CBCharacteristic]!{
            listOfServices[service.uuid.uuidString]!.append(characteristic.uuid.uuidString)
        }
    }


    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        if error != nil { print(" didUpdateValueFor:: \(error!)") }
    }

}

First class :

protocol DopiSyncCharacteristicDelegate : NSObjectProtocol {
    func dopiSyncCharacteristicData(data : NSData)
}

 //CBPeripheralDevice(Device API)
class watchBleDevice : basicDevice {

    var dopiSyncCharacteristicDelegate : DopiSyncCharacteristicDelegate!

    override init() {
        super.init()
    }

    static func getInstance() -> watchBleDevice {
        if (watchBleDevice.instance == nil) {
            watchBleDevice.instance = watchBleDevice();
        }
        return watchBleDevice.instance!;
    }

    override func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    if error != nil { print(" didUpdateValueFor:: \(error!)") }

    if characteristic.uuid == DipoDopi.dopiStreamCharacteristic.UUID {
        let data:NSData = (characteristic.value as NSData?)!;
        print("dopi stream  data :***********: \(data.hex) : \(data.length) : \(String(describing: data.hexString))")
        dopiStreamCharacteristicDelegate.dopiStreamCharacteristicData(data: data)

    }
    super.peripheral(peripheral, didUpdateValueFor: characteristic, error: error)
}
}

Second class :

class BluetoothOperations: DopiStreamCharacteristicDelegate{

    var watchBleDeviceObject : watchBleDevice!

    override init(){
        super.init()
        watchBleDeviceObject = watchBleDevice.getInstance()
        watchBleDeviceObject.dopiCharacteristicDelegate = self
    }

    func dopiStreamCharacteristicData(data: NSData) {

    }
}

Questions :

  1. Is it possible to achieve delegation between two classes ? How ?
  2. Is this method is correct ? If no, what is the correct method ?

Thanks.

回答1:

Pass instance of first class to second class.

First class :

    protocol DopiSyncCharacteristicDelegate : NSObjectProtocol {
        func dopiSyncCharacteristicData(data : NSData)
    }

     //CBPeripheralDevice(Device API)
    class watchBleDevice : basicDevice {

    var dopiSyncCharacteristicDelegate : DopiSyncCharacteristicDelegate!

    override init() {
        super.init()
    }

    static func getInstance() -> watchBleDevice {
        if (watchBleDevice.instance == nil) {
            watchBleDevice.instance = watchBleDevice();
        }
        return watchBleDevice.instance!;
    }

    override func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    if error != nil { print(" didUpdateValueFor:: \(error!)") }

    if characteristic.uuid == DipoDopi.dopiStreamCharacteristic.UUID {
        let data:NSData = (characteristic.value as NSData?)!;
        print("dopi stream  data :***********: \(data.hex) : \(data.length) : \(String(describing: data.hexString))")
      if(dopiStreamCharacteristicDelegate != nil)
      {

     dopiStreamCharacteristicDelegate.dopiStreamCharacteristicData(data: data)
      }
      else
      { print("delegate is not set yet.")
        // Passing watchBleDevice instance to BluetoothOperations class to set delegates
        BluetoothOperations().initialize(deviceInstance: watchBleDevice.getInstance())
     dopiStreamCharacteristicDelegate.dopiStreamCharacteristicData(data: data)
       }

    }
    super.peripheral(peripheral, didUpdateValueFor: characteristic, error: error)
    }
    }

Second Class :

class BluetoothOperations: NSObject , DipoCharacteristicDelegate, DopiCharacteristicDelegate, DopiSyncCharacteristicDelegate, DopiStreamCharacteristicDelegate{

    var dataToDeviceArray:[UInt8] = []
    var getWatchCollectedData: GetWatchCollectedData!
    var watchBleDeviceObject : watchBleDevice!
    private static var instance:BluetoothOperations? = nil;

    static func getInstance() -> BluetoothOperations {
        if (BluetoothOperations.instance == nil) {
            BluetoothOperations.instance = BluetoothOperations();
        }

        return BluetoothOperations.instance!;
    }
    public func initialize(deviceInstance:watchBleDevice) {
        watchBleDeviceObject = deviceInstance;

        watchBleDeviceObject.dopiSyncCharacteristicDelegate = self
    }

func dopiSyncCharacteristicData(data: NSData) {
    print("dopiSyncCharacteristicData : \(data)")
}
}


回答2:

The basic concept for delegate pattern:

protocol SomeDelegate: class {
    func doSomethig()
}

class FirstClass: NSObject {
    weak var delegate: SomeDelegate?

    func callDelegate() {
        delegate?.doSomethig()
    }
}

class SecondClass: NSObject, SomeDelegate {
    let firstClass = FirstClass()

    override init() {
        super.init()
        firstClass.delegate = self
    }

    func foo() {
        firstClass.callDelegate()
    }

    // Delegate method
    func doSomethig() {
        print("SecondClass did something")
    }
}

let bar = SecondClass()
bar.foo()

// prints "SecondClass did something"

The crash fatal error: unexpectedly found nil while unwrapping an Optional value can occur in different places of your code, because you always force unwrap optionals – you rather should use optional binding.

So answering your questions: 1. Yes it is possible 2. In which line does the crash happen?