简体   繁体   中英

Difference between [[NSDate date] retain] and [[NSDate alloc] init]

As both of the following serves the same purpose,

today = [[NSDate date] retain];    

and

today = [[NSDate alloc] init]; 

Then what's the difference between them?
Does anything here is related to memory allocation methods or something else is the reason for using them accordingly.

[NSDate date] is a convenience constructor using which you can leave the headache of releasing the object to autorelease pool. Sending a retain message to the convenience constructor like [[NSDate date] retain] makes you the owner of the object and you are responsible for releasing it properly.

[[NSDate alloc] init] is the default initializer by which you become the owner of the object, which is almost equal to [[NSDate date] retain] .

There is essentially no difference, except that the former puts the object in the autorelease pool unnecessarily.

If I want to retain the object after creating it, and an -init style method is available for the class, I almost always choose that over the convenience constructor plus -retain .

There is effectively no difference between the two. In the first example, you're just retaining an autoreleased instance created by the convenience method, which would have done something like return [[[NSDate alloc] init] autorelease] .

When you do this:

[NSDate date];

…a new NSDate is created which will automatically be released (not deallocated!) at the end of the event loop. You can, of course, retain it to keep it around longer.

When you do this:

[[NSDate alloc] init];

…a new NSDate is created which you should release when you're done with it.

From a memory management standpoint, the main differece between [[NSDate date] retain] and the alternative is that this NSDate will be around at least until the end of the event loop. If you're just creating a few objects, it doesn't matter. But, if you create (and release) a lot of objects — say, while processing data in a loop — using the former pattern could cause the memory usage of your application to spike (and then drop suddenly at the end of the event loop). With the latter pattern, the object gets destroyed as soon as you release it.

Most of the time, when a class has an autoreleased initializer - it looks like this:

return [[[NSDate alloc] init] autorelease];

So when you call [[NSDate date] retain]; , you are effectively calling

[[[[NSDate alloc] init] autorelease] retain];

Which, if you ask me, is fairly pointless - I'd just stick to [[NSDate alloc] init]; for initializing objects.

The convinience method is there so you can quickly get an autoreleased object - not to be used in conjunction with retain. It will do the same, but I would say it's better just to call the standard initialiser if you want a retained object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM