OGWNotifier Class Reference
Inherits from | NSObject |
Declared in | OGWNotifier.h |
Overview
Stores one or more receivers (delegates) in a weak collection and can broadcast selectors to each receiver. The notifier must be initialized with a protocol that defines the expected messages to be sent through the notifier.
Usage
Broadcast selectors must adhere to one of the following signatures, where ‘sender’, ‘object’ and ‘vector’ can be any selector/parameter names of your choice:
-(void) sender:(id)sender; // no object -(void) sender:(id)sender object:(id)object; // one object -(void) sender:(id)sender vector:(GWVector)vector; // for non-id objects use custom selector signature
These selectors can be broadcast via:
[notifier broadcastSelector:@selector(sender:)]; [notifier broadcastSelector:@selector(sender:object:) withObject:anObject]; [notifier broadcastSelector:@selector(sender:vector:) withVector:aVector];
For example here’s the signature for the broadcast selector sent by the OGWWorldGravityAspect through its OGWNotifier:
-(void) aspect:(OGWWorldGravityAspect*)aspect gravityDidChange:(GWVector)gravity;
The notifier can be extended with additional broadcastSelector: methods that take a non-id parameter as input. Internally the selectors are performed via objc_msgSend() which allows any type of parameter.
Here aspect is the sender and gravityDidChange is the GWVector parameter containing the new gravity vector.
Safety & Performance
In DEBUG builds the receivers are verified to conform to the protocol with which the notifier was initialized, in addition to performing respondsToSelector checks when actually performing the selector on each receiver. In release builds these checks are omitted for improved performance.
Receivers are weak references
Receivers are not retained by the notifier. This is accomplished by using NSHashTable initialized with NSPointerFunctionsWeakMemory. Hence receivers will dealloc when no other strong references hold them alive, nor does a receiver need to remove itself from the collection in order to be deallocated. From a memory-usage perspective it doesn’t matter if you “forget” to remove a receiver from a notifier.
Tasks
Protocol
-
protocol
property
Creating Notifiers
Adding/Removing Receivers
Broadcasting Selectors
Class Methods
notifierWithSender:protocol:
Creates a notifier with a sender and the protocol expected to be implemented by receivers.
+ (id)notifierWithSender:(id<OGWNotifierDelegate>)sender protocol:(Protocol *)protocol
Parameters
- sender
The sender object that will issue the perform selector broadcasts. The sender can optionally implement OGWNotifierDelegate to learn when a receiver is added or removed.
- protocol
The protocol receivers must implement. In debug configurations the receiver is tested whether it conformsToProtocol: and each notification sent is verified with respondsToSelector: - in release builds these checks are omitted for improved performance.
Return Value
The new notifier object.
Declared In
OGWNotifier.h
Instance Methods
addReceiver:
Adds a receiver. Receiver must implement the notifier’s protocol.
- (void)addReceiver:(id)receiver
Parameters
- receiver
The receiver to add.
See Also
Declared In
OGWNotifier.h
broadcastSelector:
Makes all receivers perform the given selector. The sender is sent as the first parameter.
- (void)broadcastSelector:(SEL)selector
Parameters
- selector
The selector to perform on all receivers. It must have the following signature where ‘sender’ can be any parameter name of your choice:
-(void) sender:(id)sender;
Declared In
OGWNotifier.h
broadcastSelector:withObject:
Makes all receivers perform the given selector. The sender is sent as the first parameter, and the object as the second parameter.
- (void)broadcastSelector:(SEL)selector withObject:(id)object
Parameters
- selector
The selector to perform on all receivers. It must have the following signature where ‘sender’ and ‘object’ can be any parameter name of your choice:
- object
The object to send as the selector’s second parameter.
-(void) sender:(id)sender object:(id)object;
Declared In
OGWNotifier.h
broadcastSelector:withVector:
Makes all receivers perform the given selector. The sender is sent as the first parameter, and the vector as the second parameter.
- (void)broadcastSelector:(SEL)selector withVector:(GWVector)vector
Parameters
- selector
The selector to perform on all receivers. It must have the following signature where ‘sender’ and ‘vector’ can be any parameter name of your choice:
- vector
The GWVector to send as the selector’s second parameter.
-(void) sender:(id)sender vector:(GWVector)vector;
Declared In
OGWNotifier.h
hasReceivers
YES if the notifier has one or more receivers, returns NO otherwise.
- (BOOL)hasReceivers
Return Value
YES if the notifier has one or more receivers, returns NO otherwise.
See Also
Declared In
OGWNotifier.h