[[NSDate date] retain]和[[NSDate alloc] init]之间的差异 [英] Difference between [[NSDate date] retain] and [[NSDate alloc] init]

查看:97
本文介绍了[[NSDate date] retain]和[[NSDate alloc] init]之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下两个目的相同,

  today = [[NSDate date] retain]; 

 c $ c> today = [[NSDate alloc] init]; 

那么它们之间有什么区别?

这里的任何内容都与内存有关

[NSDate date] 是一个方便的构造函数,使用它可以离开将对象释放到autorelease pool的头痛。向 [[NSDate date] retain] 的方便构造函数发送保留消息使您成为对象的所有者,



[[NSDate alloc] init] 是默认的初始化方法,成为该对象的所有者,几乎等于 c。[[NSDate date] retain] 。


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].

这篇关于[[NSDate date] retain]和[[NSDate alloc] init]之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆