[英]ViewController passed as parameter is not being deinitialized (Swift)
Setup: I have a ViewController ProblemView
and class A
.设置:我有一个ViewController ProblemView
和class A
。 I pass ProblemView
to class A
, so I can work on it.我将ProblemView
传递给 class A
,所以我可以处理它。 It looks like this (simplified):它看起来像这样(简化):
class ProblemView: UIViewController{
var instanceOfA = A()
instanceOfA.passView(passedVC: self)
}
class A{
var workOn = ProblemView()
func passView(passedVC: ProblemView){
workOn = passedVC
// I noticed, if I declare a varible locally like var workOn2 = passedVC, my problem is solved -
// but I need the variable globally, because I don't want to pass it around within this class
}
func doSth(){
// here I interact with variables of the passed ViewController
}
}
Problem: Whenever I restart this process within the app the memory increases every single time until I get memory error.问题:每当我在应用程序中重新启动此过程时,memory 每次都会增加,直到出现 memory 错误。
What I tried: I added deinit
to both classes.我尝试了什么:我在两个类中都添加了deinit
。 class A
is always deinitialized but class ProblemView
is not (this might be the problem?). class A
总是被取消初始化,但class ProblemView
不是(这可能是问题吗?)。 I also found out, that when I don't declare workOn
globally but within the passView
function, then it works just fine.我还发现,当我没有在全局范围内声明workOn
而是在passView
function 内,那么它工作得很好。 But I need have the variable globally, because I use it within many different functions of A
.但是我需要全局变量,因为我在A
的许多不同函数中使用它。 What could be a solution or workaround to this problem?什么可能是这个问题的解决方案或解决方法?
Strong references to each other.互相强引用。
Try to change class A:尝试更改 class A:
weak var workOn: ProblemView?
func passView(passedVC: ProblemView){
workOn = passedVC
// I noticed, if I declare a varible locally like var workOn2 = passedVC, my problem is solved -
// but I need the variable globally, because I don't want to pass it around within this class
}
func doSth(){
// here I interact with variables of the passed ViewController
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.