is [[NSObject alloc] release] correct?
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