On Sun, Jan 25, 2015, at 05:16 PM, Jerry Krinock wrote:
• Amount of code required is the same or less
> • Ability to coalesce and filter notifications per object or name
> • When an observer is being torn down you can remove all observers with
> one line of code,
>      [NSNotificationCenter removeObserver:self].

This is a dangerous API. If your superclass has registered for any
observations, you will unregister before your superclass hears about it.

> With KVO, if I recall correctly, you need to remember and remove each
> observance, arghhhh.

You can use the magic of C's sizeof keyword to avoid repeating yourself
for each observed keypath:

static void *observationContext = &observationContext;
static NSString *observedKeypaths[] = {@"prop1", @"prop2"};

- (void)startObservingModelObject:(MyModelObject *)obj
{
  for (int i = 0; i < sizeof(observedKeypaths) /
  sizeof(*observedKeypaths); i++)
    [obj addObserver:self forKeyPath:observedKeypaths[i] options:0
    context:observationContext];
}

- (void)stopObservingModelObject:(MyModelObject *)obj
{
  for (int i = 0; i < sizeof(observedKeypaths) /
  sizeof(*observedKeypaths); i++)
    [obj removeObserver:self forKeyPath:observedKeypaths[i]
    context:observationContext];
}

--Kyle Sluder

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to