Swift 协议委托返回 nil
Swift protocol delegate returning nil
我在 didConnect 函数中设置了一个断点来打印出委托的值,它以 nil
的形式返回,我不知道为什么
@objc protocol KestrelDeviceConnectDelegate {
func didConnect()
func didDisconnect()
}
@objcMembers
class KestrelDeviceConnect: NSObject{
static let singleton = KestrelDeviceConnect()
public weak var delegate: KestrelDeviceConnectDelegate?
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
self.isInitialReading = false
self.kestrelIsConnected = true
kestrelPeripheral.discoverServices(nil)
delegate?.didConnect()
}
}
在 viewDidLoad
在我的目的地 VC
_kestrelVC = [[KestrelDeviceConnect alloc]init];
[_kestrelVC setDelegate:self];
问题是初始化一个新的 KestrelDeviceConnect
实例,所以 _kestrelVC = [[KestrelDeviceConnect alloc]init];
有效的是 _kestrelVC = [KestrelDeviceConnect singleton]
我在 didConnect 函数中设置了一个断点来打印出委托的值,它以 nil
的形式返回,我不知道为什么
@objc protocol KestrelDeviceConnectDelegate {
func didConnect()
func didDisconnect()
}
@objcMembers
class KestrelDeviceConnect: NSObject{
static let singleton = KestrelDeviceConnect()
public weak var delegate: KestrelDeviceConnectDelegate?
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
self.isInitialReading = false
self.kestrelIsConnected = true
kestrelPeripheral.discoverServices(nil)
delegate?.didConnect()
}
}
在 viewDidLoad
在我的目的地 VC
_kestrelVC = [[KestrelDeviceConnect alloc]init];
[_kestrelVC setDelegate:self];
问题是初始化一个新的 KestrelDeviceConnect
实例,所以 _kestrelVC = [[KestrelDeviceConnect alloc]init];
有效的是 _kestrelVC = [KestrelDeviceConnect singleton]