- No need to write manual observer implementations, just inherit from JSObservableObject and everything you need to for protocol-based observation will be setup for you.
- Protocol-based observation is much easier to maintain and less prone to silent failures than, for example, key value observation.
- Works with any existing observation protocols you might have.
@protocol MyClassObserver <NSObject>
- (void)myObject:(MyClass *)object didPerformTaskWithObject:(id)anotherObject;
@end
#import "JSObservableObject.h"
JSObservableObjectInterface(MyClass, MyClassObserver)
// Your class' public API methods here
@end
This will setup your class for observation, and require all observers to implement the MyClassObserver
protocol.
MyClass *object = [MyClass new];
[object addObserver:self];
...
[object removeObserver:self];
Use the -notifyObserversWithSelector:parameters:
to notify your observers of an event. The selector should match a method declared in your observation protocol.
SEL selector = @selector(myObject:didPerformTaskWithObject:);
NSArray *parameters = @[self, anotherObject];
[self notifyObserversWithSelector:selector parameters:parameters];
The above code will send the specified selector to all of the object's observers, assigning the parameters as parameters to the call.
Why not give me a shout on Twitter: @johnsundell