Re: Cocoa class extension best practice
Re: Cocoa class extension best practice
- Subject: Re: Cocoa class extension best practice
- From: Fritz Anderson <email@hidden>
- Date: Wed, 16 Oct 2013 13:22:44 -0500
On 16 Oct 2013, at 11:57 AM, 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. … If NSMenu were my own class, I'd definitely use the instance variable for this sort of routine.
No, it isn't OK. NSMenu is not your own class, and you can't assume NeXT and Apple implemented it as you would have implemented your own class.
Your category is a third-party client of NSMenu, and it has no insight into how or whether _itemArray is used. The ivar may be there to keep the size and offsets of the struct compatible with how NSMenu was implemented twenty-plus years ago. There is no guarantee it is used now, nor that it has the same use and content as -itemArray (are dynamic items in both?), nor that it won't be abandoned or repurposed in the future (will dynamic items be in both in 2015?).
Do not access the instance variables of classes you don't own. You don't know where they've been.
(Terminology matters: There is such a thing as a "class extension," and I gather it is not what you are talking about. You're talking about a category, which is described by nothing other than the name "category." A class extension is the private continuation of the main class @interface, usually in the main implementation file, declared as though it were a category with no name.)
— F
_______________________________________________
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