1
func start() {
    var num = 1
    Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
        num += 1
    }
    Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
        print(num)
    }
}

Running the method above prints 2, 3, 4, etc every second. How does the second timer know about the current value of num if it captures its own copy of num? or does it throw num into some sort of a reference type container and actually capture the pointer to num ? Let me know if the question is unclear.

Michael Voline
  • 383
  • 3
  • 13
  • 2
    Related: https://stackoverflow.com/questions/44642073/swift-does-closure-have-references-to-constants-or-variables – dan Jan 09 '20 at 16:24
  • Quite an elegant little demonstration, nicely done. – matt Jan 09 '20 at 19:44

0 Answers0