2017-08-11 63 views
0

我在Viewcontrollers中使用委託,它們運行良好,但現在我希望在我的兩個NSObject類之間進行委派處理。 但要崩潰:fatal error: unexpectedly found nil while unwrapping an Optional value在NSObject類中的委託

下面是我在做什麼:

基本設備類

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!)") } 
    } 

} 

一流

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) 
} 
} 

二等

class BluetoothOperations: DopiStreamCharacteristicDelegate{ 

    var watchBleDeviceObject : watchBleDevice! 

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

    func dopiStreamCharacteristicData(data: NSData) { 

    } 
} 

問題

  1. 是否有可能兩個類之間實現委託?怎麼樣 ?
  2. 此方法是否正確?如果不是,那麼正確的方法是什麼?

謝謝。

+0

我不知道,但這個盤,我認爲它的工作ü... VAR watchBleDeviceObject:watchBleDevice = watchBleDevice() –

+0

你沒有初始化的變量dopiSyncCharacteristicDelegate。你只宣佈它。這就是爲什麼它崩潰。 – jegadeesh

+0

@jegadeesh是的,但如何初始化它和在哪裏? –

回答

1

將第一類的實例傳遞給第二類。

一等

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) 
    } 
    } 

二等

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)") 
} 
} 
+0

如何初始化watchBleDevice類中的dopiSyncCharacteristicDelegate?您發佈的代碼已經存在於我的代碼中。 –

+0

對不起,你不需要初始化委託對象。你在哪裏得到崩潰? – jegadeesh

+0

同時在第一類中調用委託函數。 dopiStreamCharacteristicDelegate.dopiStreamCharacteristicData(data:data) –

0

用於委託模式的基本概念:

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" 

碰撞fatal error: unexpectedly found nil while unwrapping an Optional value可以出現在代碼的不同位置,因爲你總是強制打開可選項 - 你應該使用可選的綁定。

所以回答你的問題: 1.是的,這是可能的 2.崩潰發生在哪一行?

+0

崩潰線\t 同時在第一類中調用委託函數。 dopiStreamCharacteristicDelegate.dopiStreamCharacteristicDat a(data:data) –