2

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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kiwo Tew
  • 1,491
  • 1
  • 22
  • 39
  • It's worth noting that only class types can be `weak` or `unowned` (the other ownership qualifier). Your code above will not compile since `Bool` is a struct. – Stuart Aug 07 '15 at 15:56
  • possible duplicate of [What is the difference between a weak reference and an unowned reference?](http://stackoverflow.com/questions/24011575/what-is-the-difference-between-a-weak-reference-and-an-unowned-reference) – Stuart Aug 07 '15 at 15:57
  • Probably the best guide regarding strong, weak and unowned references: https://krakendev.io/blog/weak-and-unowned-references-in-swift – Kametrixom Aug 07 '15 at 16:04

1 Answers1

3

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

Tony Thomas
  • 967
  • 10
  • 20