• 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
is [[NSObject alloc] release] correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

is [[NSObject alloc] release] correct?


  • Subject: is [[NSObject alloc] release] correct?
  • From: "Adam R. Maxwell" <email@hidden>
  • Date: Mon, 5 Jun 2006 20:02:09 -0700

Periodically, it's helpful to add an init... method in a category to return a non-autoreleased object (in NSDate, for instance, since dateWithNaturalLanguageString fills autorelease pools really quickly due to its implementation). A fellow developer and I have been discussing the appropriate way to do this, as shown in the following contrived example:

@interface MyObject : NSObject @end
@interface OtherObject : MyObject @end

@interface MyObject (Extensions) @end

@implementation MyObject (Extensions)

// Pattern A
- (id)initWithObject:(id)anObject
{
   [self release];
   return [[OtherObject alloc] init];
}

// Pattern B
- (id)initWithObject:(id)anObject
{
   [[self init] release];
   return [[OtherObject alloc] init];
}

@end

From my reading of the docs, Pattern A is correct in theory. However, it breaks badly with NSDate, and I'm curious as to whether Pattern B is needed for only a few cases ([[NSArray alloc] release] and [[NSString alloc] release] work fine, for instance), or is more correct.

thanks,
Adam
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: is [[NSObject alloc] release] correct?
      • From: Graham J Lee <email@hidden>
    • Re: is [[NSObject alloc] release] correct?
      • From: Shawn Erickson <email@hidden>
  • Prev by Date: Re: stringWithContentsOfURL: usedEncoding: error:
  • Next by Date: Re: stringWithContentsOfURL: usedEncoding: error:
  • Previous by thread: Re: stringWithContentsOfURL: usedEncoding: error:
  • Next by thread: Re: is [[NSObject alloc] release] correct?
  • Index(es):
    • Date
    • Thread