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/

 

你可能感兴趣的:(Objective-C高效编程)