有時(shí)候我們通知希望只監(jiān)聽到一次后就不再監(jiān)聽了,可以如下處理
?? __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:kNotificationName object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
??????? NSLog(@"跑了一次");
??????? [[NSNotificationCenter defaultCenter] removeObserver:observer];
??? }];
addObserverForName方法會(huì)返回一個(gè)id <NSObject>類型滨攻,可以在監(jiān)聽到通知后進(jìn)行移除跌造。