gpt4 book ai didi

ios - NSObject 类中的委托(delegate)

转载 作者:行者123 更新时间:2023-11-28 12:18:32 26 4
gpt4 key购买 nike

我在 Viewcontrollers 中使用了委托(delegate),它们运行良好,但现在我想要在我的两个 NSObject 类之间进行委托(delegate)处理。但是会崩溃: fatal error :在展开可选值时意外发现 nil

这是我正在做的:

基本设备类:

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. 是否可以在两个类之间实现委托(delegate)?怎么样?
  2. 这个方法正确吗?如果不是,正确的方法是什么?

谢谢。

最佳答案

将第一类的实例传递给第二类。

头等舱:

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

关于ios - NSObject 类中的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45629520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com