我在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) {
}
}
问题:
- 是否有可能两个类之间实现委托?怎么样 ?
- 此方法是否正确?如果不是,那么正确的方法是什么?
谢谢。
我不知道,但这个盘,我认为它的工作ü... VAR watchBleDeviceObject:watchBleDevice = watchBleDevice() –
你没有初始化的变量dopiSyncCharacteristicDelegate。你只宣布它。这就是为什么它崩溃。 – jegadeesh
@jegadeesh是的,但如何初始化它和在哪里? –