• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Custom NSArrayController - Dynamic Class?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Custom NSArrayController - Dynamic Class?
      • From: Kyle Sluder <email@hidden>
References: 
 >Custom NSArrayController - Dynamic Class? (From: Brad Gibbs <email@hidden>)
 >Re: Custom NSArrayController - Dynamic Class? (From: Quincey Morris <email@hidden>)
 >Re: Custom NSArrayController - Dynamic Class? (From: Quincey Morris <email@hidden>)
 >Re: Custom NSArrayController - Dynamic Class? (From: Brad Gibbs <email@hidden>)
 >Re: Custom NSArrayController - Dynamic Class? (From: Quincey Morris <email@hidden>)
 >Re: Custom NSArrayController - Dynamic Class? (From: Kyle Sluder <email@hidden>)
 >Re: Custom NSArrayController - Dynamic Class? (From: Quincey Morris <email@hidden>)
 >Re: Custom NSArrayController - Dynamic Class? (From: BJ Homer <email@hidden>)

  • Prev by Date: Re: [IB] - how to delete action or outlet?
  • Next by Date: Re[2]: Ideas required on testing an application install
  • Previous by thread: Re: Custom NSArrayController - Dynamic Class?
  • Next by thread: Re: Custom NSArrayController - Dynamic Class?
  • Index(es):
    • Date
    • Thread