0

I know that using a closure will always capture the properties or methods that are used inside the closure.

But do functions cause retain cycles too? If it does please explain it to me!

If not, then why do we even use closures if we are risking a retain cycle when we can just plug in a function in every closure parameter?

My question refers to whether or not a function retains memory like how a closure does.

darren zou
  • 41
  • 5
  • Closures are just anonymous functions (i.e. functions without a name) so functions will act very much the same way and carry around their local scope. More info: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html – iwasrobbed Jul 10 '17 at 03:28
  • Possible duplicate of [What is the difference between functions and closures?](https://stackoverflow.com/questions/24108667/what-is-the-difference-between-functions-and-closures) – iwasrobbed Jul 10 '17 at 03:31
  • "Global functions are closures that have a name and do not capture any values. Nested functions are closures that have a name and can capture values from their enclosing function. Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context." got it.. But no way is my question a dup because I am not an idiot and I know the diff between a closure and function. – darren zou Jul 10 '17 at 03:38

1 Answers1

0

I know that using a closure will always capture the properties or methods that are used inside the closure.

No, a function captures local variables from the surrounding scope that are used inside the function.

But do functions cause retain cycles too? If it does please explain it to me!

Functions are closures. As explained above, a function captures local variables from the surrounding scope that are used inside the function. If a function is at the top-level scope, then there are no local variables in the surrounding scope, because variables at the top-level scope are global variables.

newacct
  • 119,665
  • 29
  • 163
  • 224