我如何知道何时释放 NSDate 对象?

发布于 2024-08-20 06:48:51 字数 177 浏览 2 评论 0原文

当我完成这些日期指针中的一个或两个时,需要[释放]。我怎么知道这个?我不确定,因为我没有明确执行 init 。

NSDate *date = [NSDate date];

NSDate *date = [dateWithTimeIntervalSince1970:100000000];

Do either or both of these date pointers requires a [release] when I'm done with them. How would i know this? I'm unsure since I'm not doing init explicitly.

NSDate *date = [NSDate date];

NSDate *date = [dateWithTimeIntervalSince1970:100000000];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

呢古 2024-08-27 06:48:51

两者都是自动释放的,也就是说你不需要自己释放它们。经验法则是,如果您发送 +alloc 或 -copy 或显式保留该对象,则您拥有该对象:

  • [[SomeClass alloc] init...]
  • [someObject copy]
  • [some object keep]

如果您拥有一个对象,则必须释放它它。 +new 是+alloc 和-init 的快捷方式。

Both are autoreleased, that is you don't need to release them yourself. The rule of thumb is that you own an object if you send +alloc or -copy or explicitly retain it:

  • [[SomeClass alloc] init...]
  • [someObject copy]
  • [some object retain]

If you own an object you must release it. +new is a shortcut to +alloc and -init.

我喜欢麦丽素 2024-08-27 06:48:51

不,这些方法返回的日期都是自动释放的。你不需要担心它们的内存管理,尽管作为一个好公民,当你使用完它们时将指针设置为 nil 是一个好主意。

作为一般规则,您可以遵循我所说的“CARN”规则。在 Cocoa/Cocoa Touch 中,任何包含 Copy、Alloc、Retain 或 New 等字样的方法都将返回需要您在某个时刻释放的对象。这些是应用于返回保留计数为 +1 的对象的方法的命名约定。调用这些方法的类“拥有”该对象,并负责在使用完该对象后释放该对象。

希望这有帮助。

No, both of the returned dates from those methods are autoreleased. You don't need to worry about their memory management, though to be a good citizen, setting the pointer to nil when you're done with them would be a good idea.

As a general rule, you can follow what I call the "CARN" rule. in Cocoa/Cocoa Touch any method that has the words Copy, Alloc, Retain, or New in them will return objects that need to be released by you at some point. These are naming conventions applied to methods that return objects with a retain count of +1. The class that calls these methods "owns" the object and is responsible for releasing it when it's finished with it.

Hope this helps.

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