Re: Detecting Managed Object Property Change From Undo Redo
Re: Detecting Managed Object Property Change From Undo Redo
- Subject: Re: Detecting Managed Object Property Change From Undo Redo
- From: Kyle Sluder <email@hidden>
- Date: Mon, 26 Jan 2015 00:03:30 -0600
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 (email@hidden)
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:
This email sent to email@hidden