NSNotificationCenter 如果name是空?是什么效果?

Posted hherima

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSNotificationCenter 如果name是空?是什么效果?相关的知识,希望对你有一定的参考价值。

典型的NSNotificationCenter使用方法:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged)
                                                 name:kReachabilityChangedNotification
                                               object:nil];

下面是官方的文档

Summary

Adds an entry to the notification center's dispatch table with an observer and a notification selector, and an optional notification name and sender.

Declaration

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSNotificationName)aName object:(id)anObject;

Discussion

If your app targets ios 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc method. Otherwise, you should call removeObserver:name:object: before observer or any object passed to this method is deallocated.

Parameters

observer

Object registering as an observer.

aSelector

Selector that specifies the message the receiver sends observer to notify it of the notification posting. The method specified by aSelector must have one and only one argument (an instance of NSNotification).

aName

The name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer.

If you pass nil, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.

anObject

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.

If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

        意思是如果name,是nil那么,无论谁发送通知,都会调用这个selector。

参考文章:http://southpeak.github.io/2015/03/20/cocoa-foundation-nsnotificationcenter/

 

以上是关于NSNotificationCenter 如果name是空?是什么效果?的主要内容,如果未能解决你的问题,请参考以下文章

iOS NSNotificationCenter详解

UIApplicationWillTerminate:NSNotificationCenter 与 Application Delegate

NSNotificationCenter ,打印内存地址

WiFi网络更改是不是有NSNotificationCenter通知?

NSNotificationCenter传值

消息通信机制NSNotificationCenter -备