• 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
Cocoa class extension best practice
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Cocoa class extension best practice


  • Subject: Cocoa class extension best practice
  • From: Steve Mills <email@hidden>
  • Date: Tue, 15 Oct 2013 14:50:51 -0500

We have extended NSMenu so we could add some other methods. Many of the methods iterate over the itemArray like so:

	for(NSMenuItem* item in [self itemArray])

Instruments shows that we're leaking NSArrays created when itemArray returns. I assume whoever wrote these methods was assuming that itemArray would simply return the internal NSArray and not make a copy. I guess I would make the same assumption since it's not a copy* method.

So my question is, what's the best way to write extension methods on an existing Cocoa class like this? One way would be to assign the itemArray result to a local variable and release it when done (ARC is not turned on in this project yet):

	NSArray*		items = [self itemArray];

	for(NSMenuItem* item in items)
		blah;

	[items release];

Another would be to simply use the instance variable instead of the accessor method:

	for(NSMenuItem* item in self->_itemArray)

I don't particularly like this one since it's using a private instance var in a Cocoa-owned class, even though the code using it is technically in the same class. Or perhaps Apple might change this in the future, so it would break this code.

One other thing to consider is this: Will the itemArray ever be reallocated while we're in the middle of one of these loops? I would guess not, but one never knows how Apple implements this stuff.

Thoughts?

--
Steve Mills
office: 952-818-3871
home: 952-401-6255
cell: 612-803-6157



_______________________________________________

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: Cocoa class extension best practice
      • From: Andy Lee <email@hidden>
    • Re: Cocoa class extension best practice
      • From: Kyle Sluder <email@hidden>
    • Re: Cocoa class extension best practice
      • From: Jens Alfke <email@hidden>
    • Re: Cocoa class extension best practice
      • From: Steve Mills <email@hidden>
  • Prev by Date: Re: Scrollable GridView with fixed row and column
  • Next by Date: Re: Cocoa class extension best practice
  • Previous by thread: Re: Scrollable GridView with fixed row and column
  • Next by thread: Re: Cocoa class extension best practice
  • Index(es):
    • Date
    • Thread