简体   繁体   English

快速使用弱变量或强变量

[英]swift when to use weak or strong variables

I wonder when I should use weak and when I should use strong variables. 我想知道何时应该使用弱变量,何时应该使用强变量。

To make it simple lets say that I will hide my navigation bar when the user scrolls up. 为了简单起见,假设我将在用户向上滚动时隐藏导航栏。

Then I will set up a function to hide the bar once the user is scrolling up. 然后,我将设置一个函数,以在用户向上滚动时隐藏该栏。 But I can set a boolean value to check if the bar already is hidden, if so then its no need to run the function to hide the navigation bar. 但是我可以设置一个布尔值来检查该栏是否已被隐藏,如果是这样,则无需运行该功能来隐藏导航栏。 And also change that boolean when needed. 并在需要时更改该布尔值。

So would that kind of variable be a weak or strong one? 那么,该变量是弱变量还是强变量?

weak var isHidden = false
var isHidden = true

Use weak if that object has a life out side that reference.That is if it has a strong reference somewhere else. 如果该对象的引用超出生命范围,请使用弱。即在其他位置具有强引用。 Use strong , when that particular reference determines the life of that object 当特定引用确定该对象的寿命时,请使用strong

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM