NSDate init 问题,与 Objective-C 中的内存管理相关
我有一个由 Later 创建的 NSDate 对象
NSDate *date = [[NSDate alloc] init];
,我想将日期重置为“现在”,所以我认为
[date init];
or
date = [date init];
可以完成这项工作,但他们没有。 相反,
[date release];
date = [[NSDate alloc] init];
有效。 我对此有点困惑,因为在 - (id) init
的文档中,它说:
返回一个初始化为当前日期和时间的 NSDate 对象。
既然 date
已经分配了,难道它不应该只需要一个 init
消息吗?
I have an NSDate
object created by
NSDate *date = [[NSDate alloc] init];
Later, I want to reset the date to the "now", so I thought that
[date init];
or
date = [date init];
might do the job, but they don't. Instead,
[date release];
date = [[NSDate alloc] init];
works. I'm a bit confused about this, since in the documentation for - (id) init
, it says:
Returns an NSDate object initialized to the current date and time.
and since date
is already allocated, shouldn't it just need an init
message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
alloc
和init
视为构造函数逻辑上不可分割的两部分。 您只能对给定对象调用一次以“init”开头的方法 - 一旦该对象已初始化,则再次初始化它是错误的。 对于任何 Objective-C 对象都是如此,而不仅仅是 NSDate。 然而,NSDate 对象也是不可变的——一旦创建,它们就无法更改。后一个代码起作用的原因是因为您正在创建 NSDate 的新实例,这是正确的做法。 您还可以使用 [NSDate date] 来完成同样的事情。 请注意,它返回一个您尚未拥有的对象,因此如果您需要保留它,则需要保留它,并稍后释放它。
请注意,如果您从某人那里收到一个对象,那么它已经被初始化了。 (如果不是,则说明提供它的代码中存在编程错误,或者是该规则的极其罕见的异常。)
Think of
alloc
andinit
as logically inseparable halves of a constructor. You can only call methods beginning with "init" once on a given object — once the object has been initialized, and it's an error to initialize it again. This is true for any Objective-C object, not just NSDate. However, NSDate objects are also immutable — once created, they can't change.The reason the latter code works is because you're creating a new instance of NSDate, which is the correct thing to do. You can also use [NSDate date] to accomplish the same thing. Be aware that it returns an object that you don't (yet) own, so you'll need to retain it if you need to keep it around, and release it later.
Be aware that if you receive an object from someone, it has already been initialized. (If not, it's a programming error in the code that provided it, or is an extremely uncommon exception to the rule.)
如果你想获取当前日期,你可以使用:
如果你想保留它,那么
retain
它。你不能用init重置
NSDate
,init仅用于第一次初始化对象。您可以获取另一个日期:
date
消息返回NSDate
的autoreleased
实例,因此release
或自动释放
。autorelease
用于您不想担心需要在何处释放对象的情况 - 它被放入autorelease pool
中。 自动释放池中的对象会在事件循环迭代结束后释放,或者当您在池上调用release时释放...(有关内存管理的更多信息,请参阅 Apple 文档)。顺便提一句。
[NSDate date]
是一种方便的方法,它可能类似于(不能保证完全相同,但功能相似):If you want to get the current date you can just use:
If you want to keep it then
retain
it.You can't reset
NSDate
with init, init is only for initializing the object for the first time.You could just get another date:
The
date
message returnsautoreleased
instance ofNSDate
, hence therelease
orautorelease
.The
autorelease
is used for cases where you don't want to worry about where exactly you need to release the object - it is put intoautorelease pool
. Object in autorelease pool are released after the end of event loop iteration, or when you call release on pool ... (see more in Apple docs about memory management).Btw. the
[NSDate date]
is a convenience method it's probably something like (not quaranteed to be exactly the same but functionally similar to):