NSDate init 问题,与 Objective-C 中的内存管理相关

发布于 2024-07-30 03:49:41 字数 522 浏览 4 评论 0原文

我有一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朮生 2024-08-06 03:49:41

allocinit 视为构造函数逻辑上不可分割的两部分。 您只能对给定对象调用一次以“init”开头的方法 - 一旦该对象已初始化,则再次初始化它是错误的。 对于任何 Objective-C 对象都是如此,而不仅仅是 NSDate。 然而,NSDate 对象也是不可变的——一旦创建,它们就无法更改。

后一个代码起作用的原因是因为您正在创建 NSDate 的新实例,这是正确的做法。 您还可以使用 [NSDate date] 来完成同样的事情。 请注意,它返回一个您尚未拥有的对象,因此如果您需要保留它,则需要保留它,并稍后释放它。

请注意,如果您从某人那里收到一个对象,那么它已经被初始化了。 (如果不是,则说明提供它的代码中存在编程错误,或者是该规则的极其罕见的异常。)

Think of alloc and init 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.)

树深时见影 2024-08-06 03:49:41

如果你想获取当前日期,你可以使用:

NSDate * now = [NSDate date];

如果你想保留它,那么retain它。

NSDate * now = [[NSDate date] retain];

你不能用init重置NSDate,init仅用于第一次初始化对象。

您可以获取另一个日期:

NSDate * now = [[NSDate date] retain];
// use the now object
// need new date
[release now];
now = [[NSDate date] retain];
// once you don't need it release it
[now release];

date 消息返回 NSDateautoreleased 实例,因此 release自动释放

autorelease 用于您不想担心需要在何处释放对象的情况 - 它被放入 autorelease pool 中。 自动释放池中的对象会在事件循环迭代结束后释放,或者当您在池上调用release时释放...(有关内存管理的更多信息,请参阅 Apple 文档)。

顺便提一句。 [NSDate date] 是一种方便的方法,它可能类似于(不能保证完全相同,但功能相似):

- (NSDate *)date
{
    return [[[NSDate alloc] init] autorelease];
}

If you want to get the current date you can just use:

NSDate * now = [NSDate date];

If you want to keep it then retain it.

NSDate * now = [[NSDate date] retain];

You can't reset NSDate with init, init is only for initializing the object for the first time.

You could just get another date:

NSDate * now = [[NSDate date] retain];
// use the now object
// need new date
[release now];
now = [[NSDate date] retain];
// once you don't need it release it
[now release];

The date message returns autoreleased instance of NSDate, hence the release or autorelease.

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 into autorelease 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):

- (NSDate *)date
{
    return [[[NSDate alloc] init] autorelease];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文