我应该在变量中使用弱自我吗?

时间:2022-09-20 10:51:36

I have case where my property getter is referencing to the self and in the way I have suspicion that it will hold self and will never release it.Could you please explain if I am not making mistake in this case?

我的情况是,我的财产获取者引用了自我,并且我怀疑它会保留自己并且永远不会释放它。如果我在这种情况下没有犯错,请你解释一下吗?

 private var storyboard: UIStoryboard {
    get {
      return UIStoryboard(name: "Login", bundle: Bundle(for: type(of:self)))
    }
 }

1 个解决方案

#1


2  

You're not making a mistake – a computed property is no more than a glorified method / pair of methods. On its own, there are no retain cycle concerns.

你没有犯错 - 计算属性只不过是一种美化的方法/一对方法。就其本身而言,没有保留周期问题。

self is simply an implicit parameter passed upon calling the getter or setter of the computed property, and will be the instance that the property is being accessed on – it's not stored or captured by the property itself.

self只是在调用计算属性的getter或setter时传递的隐式参数,并且将是访问该属性的实例 - 它不是由属性本身存储或捕获的。

#1


2  

You're not making a mistake – a computed property is no more than a glorified method / pair of methods. On its own, there are no retain cycle concerns.

你没有犯错 - 计算属性只不过是一种美化的方法/一对方法。就其本身而言,没有保留周期问题。

self is simply an implicit parameter passed upon calling the getter or setter of the computed property, and will be the instance that the property is being accessed on – it's not stored or captured by the property itself.

self只是在调用计算属性的getter或setter时传递的隐式参数,并且将是访问该属性的实例 - 它不是由属性本身存储或捕获的。