On Jul 10, 2008, at 4:55 PM, Patrick Mau wrote:

I'd like to show you one ugly detail. I put all observed objects,
including their keyPath in a local NSDictionary, because I have to
remove the observers on deletion of a table row. The 'observeObject'
looks like that:

- (void)observeObject:(id)object withKeyPath:(NSString *)keyPath {
   NSLog(@"start observing %p with keypath %@", object, keyPath);

   [object addObserver:self forKeyPath:keyPath
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

[observers setValue:object forKeyPath:[NSString stringWithFormat:@"%p.%@", object, keyPath]];
}

In my opinion, this is as ugly as it can possibly get.

I'm using the address of the observed object and append the keypath to
create a pseudo key for each observed instance.

I don't like that at all, but how would I be able to remove all
observers when I have to clean them up later?

Is there a better approach instead of observing every property of all
array entries?

How about a dictionary whose keys are NSValues representing the objects? For each object, the value from the dictionary would be an NSMutableArray whose elements are the key paths being observed on that object.

        // ...
        NSValue* objectKey = [NSValue valueWithNonretainedObject:object];
        NSMutableArray* keyPaths = [observers objectForKey:objectKey];
        if (!keyPaths)
        {
                keyPaths = [NSMutableArray array];
                [observers setObject:keyPaths forKey:objectKey];
        }
        [keyPaths addObject:keyPath];


Cheers,
Ken
_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to