我需要“保留”吗?将现有对象存储在控制器实例变量中时?

发布于 2024-11-09 04:40:05 字数 593 浏览 0 评论 0原文

我对 Objective-C 相当陌生,正在尝试掌握复杂的世界或内存管理。

我正在构建一个 iPhone 应用程序,并且有一个供各种视图控制器使用的全局单例对象。

我试图缩短每次访问此全局对象时必须编写的代码,并希望将此对象的引用存储为视图控制器中的实例变量。

我的视图控制器 .h 文件:

@interface MyViewController : UIViewController {

    MyGlobalObjectType *_myobject;
}

@end

这是我的视图控制器 .m 文件的 init 方法:

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder])) {        
        _myobject = [[Global sharedGlobal] myobject];
    }
    return self;    
}

我的问题是,当我存储对另一个变量的引用(指针)时,在初始化该实例变量时是否必须对其调用保留?

I'm fairly new to Objective-C and am trying to get to grips with the complex world or memory management.

I'm building an iPhone app and have a global singleton object that is used by various view controllers.

I'm trying to shorten the code I have to write each time I access this global object and want to store a reference to this object as an instance variable in the view controllers.

My view controller .h file:

@interface MyViewController : UIViewController {

    MyGlobalObjectType *_myobject;
}

@end

This is my init method of my view controller .m file:

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder])) {        
        _myobject = [[Global sharedGlobal] myobject];
    }
    return self;    
}

My question is when I store a reference (pointer) to another variable do I have to call retain on it when initialising that instance variable?

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

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

发布评论

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

评论(2

挽你眉间 2024-11-16 04:40:05

一般来说,没有。使用单例对象进行内存管理可能是一件棘手的事情,但通常,您希望单例在应用程序的整个过程中初始化一次,并且在应用程序退出之前不会释放。

在这种情况下,当你的对象(在单例内部)被初始化时,它的引用计数为+1。如果您要对该对象调用retain,则其引用计数将增加到+2。这是一件完全可以做的事情,只要你记得在视图控制器完成它时调用 release ,使其计数回到+1。也就是说,您通常不需要这样做。

换句话说,请记住具有引用计数的是对象。指向对象的指针则不然,因此无需发送 retain 消息来保持该对象处于活动状态。

In general, no. Memory management with a singleton object can be a tricky thing to get right, but usually, you want the singleton to be initialized once throughout the course of the application and not deallocated until the application quits.

In this case, when your object (inside the singleton) gets initialized, it has a reference count of +1. If you were to call retain on that object, its reference count would go up to +2. This is a perfectly okay thing to do as long as you remember to call release on it when your view controller is done with it, bringing its count back down to +1. That said, you usually don't need to do this.

In other words, remember that it is the object that has the reference count. Pointers to the object do not, so there's no need to send a retain message in order to keep this object alive.

最冷一天 2024-11-16 04:40:05

为什么你想要保存对你的单身人士的引用?这从一开始就违背了创建一个的目的。只需在需要时获取对单例的引用即可。

Why would you want to hold a reference to your singleton? That defeats the purpose of creating one in the first place. Just get a reference to singleton at the moment you need it.

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