Swift协议委托返回nil

时间:2019-07-01 19:10:04

标签: ios swift delegates protocols

我在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()

    }

}

在我的目标VC的viewDidLoad

_kestrelVC = [[KestrelDeviceConnect alloc]init];
[_kestrelVC setDelegate:self];

1 个答案:

答案 0 :(得分:0)

问题是初始化一个新的KestrelDeviceConnect实例,所以没有_kestrelVC = [[KestrelDeviceConnect alloc]init];有效的是_kestrelVC = [KestrelDeviceConnect singleton]