简体   繁体   English

[[NSDate date] retain]和[[NSDate alloc] init]之间的区别

[英]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. [NSDate date]是一个方便的构造函数,您可以使用它来解释将对象释放到自动释放池。 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. retain消息发送到方便构造函数(如[[NSDate date] retain]会使您成为对象的所有者,并且您有责任正确地释放它。

[[NSDate alloc] init] is the default initializer by which you become the owner of the object, which is almost equal to [[NSDate date] retain] . [[NSDate alloc] init]是您成为对象所有者的默认初始值设定项, 几乎等于 [[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 . 如果我想在创建对象后保留该对象,并且该类可以使用-init样式方法,我几乎总是选择方便构造函数加-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] . 在第一个例子中,你只是保留了一个由easy方法创建的自动释放的实例,它会做类似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. ...创建一个新的NSDate,它将在事件循环结束时自动释放(不释放!)。 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. ...创建一个新的NSDate,您应该在完成后释放它。

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. 从内存管理的角度来看, [[NSDate date] retain]和替代方案之间的主要区别是这个NSDate将至少在事件循环结束之前。 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]; 所以当你调用[[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]; 如果你问我,这是毫无意义的 - 我只是坚持[[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. 方便的方法就在那里,所以你可以快速获得一个自动释放的对象 - 不要与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. 它会做同样的事情,但我想说如果你想要一个保留的对象,最好只调用标准初始化器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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