Ios 我不知道';我不明白为什么我的对象没有';我没有收到通知

Ios 我不知道';我不明白为什么我的对象没有';我没有收到通知,ios,swift,memory-management,nsnotificationcenter,Ios,Swift,Memory Management,Nsnotificationcenter,我创建了一个自定义类,它看起来像: 类设备:设备{ 私有变量控制器:FooController? 私有变量设备:Foo? 重写init(){ super.init() 如果超级授权{ NotificationCenter.default.addObserver(self,selector:#selector(self.discovered(:)),name:NSNotification.name(rawValue:FooDiscover),object:nil) NotificationCent

我创建了一个自定义类,它看起来像:

类设备:设备{
私有变量控制器:FooController?
私有变量设备:Foo?
重写init(){
super.init()
如果超级授权{
NotificationCenter.default.addObserver(self,selector:#selector(self.discovered(:)),name:NSNotification.name(rawValue:FooDiscover),object:nil)
NotificationCenter.default.addObserver(self,selector:#selector(self.connected(:)),name:NSNotification.name(rawValue:FooConnected),object:nil)
}
}
@发现objc专用func(uu通知:通知){
DDLogVerbose(“FOO-discovered-\(通知)”)
super.scanner?.stopScanFor(.\u FOO)
//TODO:调用super.connector并连接
}
@objc专用func已连接(uu通知:通知){
DDLogVerbose(“FOO-connected-\(通知)”)
//连接后做更多的事情
}
func start(){
DDLogVerbose(“FOO-startMeasurement()”)
超级扫描仪?.scanFor(.\u FOO)
}  
}
超级类看起来像:

类设备:NSObject{
私人出租许可证=“我的许可证”
私有var验证器:SDKAuthenticator?
内部var扫描仪:SDKScanner?
内部var连接器:SDKConnector?
内部变量AUTHPROGRATED=false
重写init(){
super.init()
authgrated=self.authRequest(许可证)
如果批准{
scanner=SDKScanner.getInstance()
connector=SDKConnector.getInstance()
}否则{
//TODO:向用户显示错误
}
}
private func authRequest(u数据:字符串)->Bool{
//使用authenticator和authenticated做一些事情,我们可以假设我们返回了一个true
返回状态//true
}
}
在我的ViewController中,我创建了一个
FooDevice
的实例并启动该过程。我正在做以下工作:

类MyViewController:UIViewController{
//许多财产
覆盖viewDidLoad(){
//查看下载内容
}
@iAction func connectToDevice(\发送方:任意){
//在这里,我实例化对象并开始扫描
让myFooDevice=FooDevice()
myFooDevice.start()
}
}
在控制台中,我可以看到扫描仪如何启动并找到蓝牙设备,但没有捕获通知,也没有打印日志。我确信通知名称是正确的,因为SDK返回字符串


我不知道我错过了什么。希望您能对此有所启发。

您的问题是ARC将在任何通知到达之前清理您的
myFooDevice
变量

您最好将其存储在属性中:

class MyViewController:UIViewController {

    var myFooDevice:FooDevice?

    override viewDidLoad() {
       // ViewDidLoad stuff
    }

    @IBAction func connectToDevice(_ sender: Any) {
        // Here I instantiate the object and start the scanning
        myFooDevice = FooDevice()
        myFooDevice!.start()
    }
}

您的问题是ARC将在任何通知到达您的
myFooDevice
变量之前清除该变量

您最好将其存储在属性中:

class MyViewController:UIViewController {

    var myFooDevice:FooDevice?

    override viewDidLoad() {
       // ViewDidLoad stuff
    }

    @IBAction func connectToDevice(_ sender: Any) {
        // Here I instantiate the object and start the scanning
        myFooDevice = FooDevice()
        myFooDevice!.start()
    }
}