iPhone - testing if a notification exists

前端 未结 2 1551
感动是毒
感动是毒 2021-01-07 18:56

At some point in a code one may add something like

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething) name:@\"Hello\" obj         


        
2条回答
  •  没有蜡笔的小新
    2021-01-07 19:15

    If you mean "testing for whether you've already registered as an observer for the notification", I don't think there's an easy way apart from posting the notification and seeing you get a callback (with possibly disastrous effects).

    If there's a danger of a double-add, I usually use [[NSNotificationCenter defaultCenter] removeObserver:self name:foo object:bar] before the add.

    Registering for notifications does not happen on a queue.

提交回复
热议问题