Re: Cocoa class extension best practice
Re: Cocoa class extension best practice
- Subject: Re: Cocoa class extension best practice
- From: Andy Lee <email@hidden>
- Date: Wed, 16 Oct 2013 13:45:49 -0400
On Oct 16, 2013, at 12:57 PM, Steve Mills <email@hidden> wrote:
> So at this point, let's finish this thread by going back to my original question. Is it OK to use the private instance variable _itemArray in NSMenu since the methods we've added are *extensions* of NSMenu and not a subclass? They simply iterate over the array and ask each item for its tag and call the same method on submenus. Below is the way it is now, with [self itemArray] being called, which seems inefficient because it creates an immutable copy of the array.
I still don't see what mutability has to do with it, but that's a side issue.
> If NSMenu were my own class, I'd definitely use the instance variable for this sort of routine.
I would too.
I suspect you can safely use the instance variable, but if you're concerned about the fact that it's undocumented, or that Apple could in theory move it to the @implementation so that it's no longer visible to your code, you can accomplish the same using numberOfItems and itemAtIndex:, as I mentioned earlier. Then you're using totally public API *and* this particular method no longer instantiates any objects. Either way, I'd add a clarifying comment.
--Andy
>
> - (NSMenuItem*)itemWithTag:(NSInteger)tag searchSubmenus:(BOOL)searchSubmenus depthFirst:(BOOL)depthFirst
> {
> if(!depthFirst) {
> id item = [self itemWithTag:tag];
>
> if(item)
> return item;
> }
>
> if(searchSubmenus) {
> for(NSMenuItem* item in [self itemArray]) {
> if([item hasSubmenu]) {
> NSMenuItem* subitem = [[item submenu] itemWithTag:tag searchSubmenus:searchSubmenus depthFirst:depthFirst];
>
> if(subitem)
> return subitem;
> }
> }
> }
>
> return [self itemWithTag:tag];
> }
_______________________________________________
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