RE: [[Class alloc] init] preferable over [[Class instance] retain]?
RE: [[Class alloc] init] preferable over [[Class instance] retain]?
- Subject: RE: [[Class alloc] init] preferable over [[Class instance] retain]?
- From: Jeff Laing <email@hidden>
- Date: Tue, 9 Mar 2010 23:58:55 +0000
- Acceptlanguage: en-US
- Thread-topic: [[Class alloc] init] preferable over [[Class instance] retain]?
> For example, is:
>
> NSMutableArray *anArray = [[NSMutableArray array] retain];
>
> better than:
>
> NSMutableArray *anArray = [[NSMutableArray alloc] init];
I've been reamed, in the recent past, for expressing the opinion that the first was just as good as the second - apparently the answer depends on what platform you're on.
The upshot was that apparently the iPhone developers like you to avoid the use of autorelease pools where possible. One hidden difference in the above is that in the first case you have (probably) populated the autorelease pool and in the other you (probably) haven't.
I say probably because you can't know for sure in the majority of cases (its not explicitly documented). And there are aberrant cases like NSDateFormatter whose -init creates a bunch of autoreleased objects that you never see.
(When worrying about memory usage, don't blindly trust documentation or heresay. Use Instruments!)
Orthogonally, there are classes like NSPredicate where you don't have a choice - there are only convenience methods, no initPredicateWithFormat:
_______________________________________________
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