未调用deinit的说明 [英] explanation for deinit not called

查看:89
本文介绍了未调用deinit的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器(用于登录),它定义了deinit方法来删除观察者.当用户点击注册"按钮时,控制器将调用performSegueWithIdentifier来加载注册视图控制器.我定义了deinit方法来删除所有观察者.但是,没有调用该方法.我在某个地方读到它是因为viewcontroller没有被破坏,并且指向它的指针被保存在某个地方.谁能解释这背后的原因?

I have a view contoller (for log in) that defines deinit method to remove observers. When user taps on Register button, the controller calls performSegueWithIdentifier to load registration view controller. I defined deinit method to remove all observers. However, that method is not being called. I read it somewhere that it is because the viewcontroller is not being destroyed and the pointer to it is hold somewhere. Could anyone explain the reason behind this?

谢谢

尽管我同意注释部分中提供的链接包含相同的问题,但是没有答案或明确的答案.最高评价的答复建议在deinit中定义某些内容,而不是将其保留为空白.那并不能解决我要寻找的爆炸问题.因此,我会一直保留这个问题,直到有人指出我的理解不正确为止.此外,我确实认为Matt可以简洁明了地解释它.

EDIT : Although I agree that the link provided in the comment section holds the same question, there is no answer or clear answer to that. The highest upvoted reply suggested to define something inside deinit instead of leaving it blank. That does not answer the explination I'm looking for. For that reason, I'm keeping this question until somebody can point out my understanding is incorrect.And also, I do think that Matt explains it succinctly and clearly.

推荐答案

在预期的情况下未能触发deinit的常见原因是您有一个保留周期,这会阻止视图控制器运行不存在.

The usual reason for failure to trigger deinit when expected is that you have a retain cycle that prevents your view controller from going out of existence.

(有时,原因是您期望在这种情况下会破坏视图控制器是不正确的.但是,假设它是正确的,则是一个保留周期.)

(Sometimes the reason is that your expectation that the view controller would be destroyed under the circumstances is incorrect. But assuming it is correct, a retain cycle is the reason.)

您提到删除所有观察员.什么样的观察员?如果我们谈论的是NSNotification,那么通常这就是您获得保留周期的方式.通知中心将保留观察者,直到您取消注册为止.因此,在观察者之后被移除之前,您无法获得deinit.因此,您无法删除 in deinit中的观察者.

You mentioned removing all observers. What kind of observers? If we're talking about NSNotification, that is often how you get a retain cycle. The notification center retains the observer until you unregister it. You thus cannot get deinit until after the observer has been removed. Therefore, you cannot remove the observer in deinit.

这篇关于未调用deinit的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆