问题标签 [deinit]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
332 浏览

ios - View controller retaining another when changing rootViewController

I've found a memory leak in an app where the following is done:

Imagine two view controllers, each of which calls a function in the appDelegate similar to this:

So far this is working well - the other VC is shown and the one that called the function is deallocated.

But when you present a third view controller from first or second VC via: present(ThirdViewController(), animated: true)

And then call the above function in the appDelegate from ThirdVC (to show viewController one or two by making it the rootViewController), this ThirdVC and the VC that presented it do not get deallocated.

Any idea why that is?

Can post sample project if needed. VC's are instantiated from storyboard if that makes any difference.

0 投票
2 回答
1678 浏览

ios - Swift iOS - 当作为子视图添加到另一个视图控制器时,是否应该在子视图控制器中调用 Deinit?

我在另一个 parentVC(vc1) 中的 parentVC(vc2) 中有一个 childVC(vc3)。我这样做是出于动画目的。

发生的事情是我将 vc3 作为孩子添加到 vc2。我有一个推动 vc1 的 collectionView。一旦 vc1 出现在场景中,就会将 vc2 添加到其中。当我将 vc1 从堆栈中弹出并返回到 collectionView 时,vc1 中的 deinit 会被调用,但是 vc2 中的 deinit 永远不会被调用。

vc2 中的 deinit 是否应该被调用,即使它是 vc1 的孩子?或者可能是因为 thirdVC 在 secondVC 内部创建了对自身的强引用?

在其中添加了 ThirdVC 的 SecondVC:

集合视图:

第一VC:

0 投票
1 回答
2123 浏览

swift - 在对象 deinit 的扩展中调用代码?

在 Swift(我使用的是 4.1)中,当对象被破坏时,有没有办法在扩展中进行一些清理?我想到了会进入的那种代码deinit(),但是扩展不能声明deinit()。(除此之外,如果需要多个扩展来执行此操作,则会有多个deinit()声明。)

0 投票
1 回答
92 浏览

ios - Swift iOS -View Controller deinit 在将其添加到新的 keyWindow 时运行

我有一个视图控制器 (OrangeVC),我将它添加到一个包含新 keyWindow(NewKeyWindowClass) 的类中。点击不同 vc 中的一个按钮,它会触发这个新窗口以显示在应用程序的主窗口上,并从屏幕的右侧底部动画填充到顶部。动画效果很好,它从底部开始,用橙色背景的新 vc 填充屏幕。问题是一旦将 OrangeVC 添加到 NewKeyWindowClass 中,orangeVC 的 deinit 就会不断被触发。

为什么它在运行 deinit?

进入 Animator 类的类:

动画师类:

来自触发动画的不同类的按钮:

0 投票
1 回答
143 浏览

ios - Deinit not called where blocks used in swift 3.0

I have used blocks in cell for getting switch value but now my problem is that deinit not called where i used the blocks. it is completely working previously but in swift 3.0 it is not working.

My switch cell :

Uses of this cell

Also if i comment this two blocks then my deinit will called.

0 投票
2 回答
1085 浏览

swift - 为什么没有在 UITabBarController 上调用 deinit?

我正在学习 Swift。我不会说我是新手,但我敢肯定,就像许多在线学习一样,我错过了很多了解实际情况的基本步骤。

在我的应用程序走得很远之后,我现在发现我的内存管理很差。我正在使用 SDWebImage 缓存,它肯定会受到 GIFS 的影响,但更重要的是,我现在正在学习保留周期和 deinit。

有人可以解释为什么 UITabBarController deinit 中的 ViewController 永远不会被调用吗?为什么这不是一件坏事?(除非它是)并且只是使用标签栏控制器时有关内存管理的一般建议/方向。我已经研究了保留周期以及它们为什么会引起和修复,但根据 xCodes 工具工具,这似乎不是我的问题。

任何建议将不胜感激

谢谢。

0 投票
1 回答
564 浏览

swift - 我可以在 deinit 中使用 didSet 吗?

我在我的类中添加了一个 Timer 变量,并使用它的 didSet 观察者使旧值无效

我认为这足以在类被取消初始化时使计时器无效,但看起来没有调用 didSet。这是为什么?观察者在反初始化期间不工作吗?

0 投票
2 回答
243 浏览

swift - deinit 未在样板 macOS 应用程序上调用

我已经从 macOS Cocoa App 模板创建了新的应用程序。我所做的唯一更改是添加deinitNSViewController. 所以现在它看起来像这样(完整代码):

为什么我看不到deinit电话?我在这里搜索了许多问题,但找不到答案,因为我没有任何保留周期。

0 投票
1 回答
647 浏览

ios - Swift - 调用 .removeObserver 的正确位置是否总是 deinit()?

这里是新的 StackOverflow 用户(第一次发帖,长时间潜伏在没有帐户的情况下)。在开始之前,这些是我之前回答过的一些问题,我发现它们很有帮助,但还没有完全解决我的问题:

如何安全地移除观察者(Swift)

为 NSNotificationCenter = Swift deinit() 调用 .removeObserver 的正确位置?

从这些我构建了一个 BaseView 控制器,用它来控制我的应用程序在各种情况下的行为(例如,当应用程序回到前台时检查更新的 API 调用)

但是,我的问题是我需要使用其他 NotificationCenter 观察者来动态控制导航(进度)栏,该栏取决于用户在应用程序中的位置(以及他们在那里做什么,与其他区域隔离)。

我的问题是:“调用 .removeObserver 的正确位置是否总是 deinit()?” 或者,如果没有,是否有任何关键的地方应该考虑添加 .removeObserver 调用?

如果有帮助,应用程序每个部分的导航栏都附加到(a) 上,该MainPagerVC(a UIPageViewController) 可通过LGSideMenuController重用和切换进出

0 投票
1 回答
60 浏览

swift - ARC 如何作用于强制展开的实例

我正在尝试了解 ARC 的工作原理,因此我阅读了Swift ARC 文档,并按照他们在使用 Playground 的文档中提供的示例进行操作:

我的问题是为什么当我调用Person实例john可选时:

对象被释放,但是当我调用它时强制解包:

它不会被释放。