独立重用已声明的强大属性

发布于 2024-12-27 04:53:00 字数 450 浏览 1 评论 0原文

我创建了一个类(MyClass)并需要它的多个实例,每个实例将包含多个计时器、文本字段和标签。由于 ARC,当计时器失效时,目标就会被释放,但有时我会让它们失效并重新启动它们,所以我不能让它们释放。因此,我转到 AppDelegate(这是创建 MyClass 实例的类)并将 MyClass 声明为强属性。 @property (strong) MyClass *myInstance; 这部分解决了问题,每当我创建另一个实例时,前一个实例就会丢失它的引用,并且如果我尝试在旧实例中重新启动 NSTimer,我访问错误。如果我重新启动最后计时器,那就没有问题了。

我相信,由于 myInstance 是一种属性,每当我创建一个新属性时,AppDelegate 都会重写旧的属性,从而丢失旧的引用。我需要能够保留强属性,但以某种方式使其对每个实例独立工作,或者找到另一种方法使 myInstance 成为强引用,而不必是属性。

I created a class (MyClass) and need several instances of it, each will hold several timers, textfields and labels. Because of ARC, the target was getting deallocated when the timers were invalidated, but I sometimes invalidate them to restart them so I can't let them deallocate. So I went to my AppDelegate (which is the class that creates the instances of MyClass) and declared MyClass as a strong property. @property (strong) MyClass *myInstance; This partially works the problem is that whenever I create another instance, the previous instance loses it's reference, and if I try to restart the NSTimer in an old instance, I get BAD ACCESS. If I restart the last timer there's no problem.

I believe that since myInstance is a property, whenever I make a new one the AppDelegate rewrites the old one, losing the old references. I need to either be able to keep the strong property but somehow make it work independently for each instance, or find another way to make myInstance a strong reference, without it having to be a property.

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

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

发布评论

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

评论(1

牵你手 2025-01-03 04:53:00

将您的实例放入 NSSet 或 NSArray 等容器中。

Put your instances in a Container like NSSet or NSArray.

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