Re: Custom NSArrayController - Dynamic Class?
Re: Custom NSArrayController - Dynamic Class?
- Subject: Re: Custom NSArrayController - Dynamic Class?
- From: BJ Homer <email@hidden>
- Date: Sun, 19 Jul 2009 10:40:40 -0600
On Jul 18, 2009, at 11:52 PM, BJ Homer <email@hidden> wrote:
On Sat, Jul 18, 2009 at 4:23 PM, Quincey Morris <email@hidden
> wrote:
On Jul 18, 2009, at 15:07, Kyle Sluder wrote:
I would instead recommend using -setValue:forKey: like this:
[object setValue:[NSNumber numberWithInteger:[[self arrangedObjects]
indexOfObject:object]] forKey:@"index"]
Yes, it's more sensible.
But now that I think about it, the "performSelector" approach has
one *slight* advantage to the developer. If you ever use Xcode's
refactoring to change the name of the property, it will miss the
property name in the string. With @selector, the method name still
doesn't change, but the refactor window does give a warning that
it's not going to change automatically.
Perhaps the best option is option (c): create a IndexedObject
abstract superclass (if there isn't one already) and use
'object.index = ...' after all.
In order to preserve the contract of NSArrayController (which is
that you can add any object with addObject:), I'd recommend doing
something like this:
- (void)addObject:(id)object {
if ([object respondsToSelector:@selector(setIndex:)] {
object.index = [NSNumber numberWithInt:[[self arrangedObjects]
indexOfObject:object]];
}
[super addObject:object];
}
Note that I call super's addObject: at the end. I have no idea what
the implementation of NSArrayController's addObject: is, but it's
always better to have things set up before you pass something along
to super. Imagine, for example, that NSArrayController writes the
object immediately to disk when added. Since you haven't set your
index yet, it would be incorrect. (I don't think it actually writes
anything to disk at that point, but you get the idea.)
-BJ
Actually, use [object setIndex:] instead, and cast it as an
IndexedObject*. That will get rid of your compiler warnings.
-BJ
_______________________________________________
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