Advanced Swift 3 - Part 12: ARC with Closures | Ray Wenderlich

As reference types, closures face the same challenges that classes do when it comes to memory management. Use weak and unowned with closure captures to prevent leaks.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3810-advanced-swift-3/lessons/12

Hi Ray, thanks for your awesome tut. quick question, if we wanna run print(galileo.name) then we should @escaping for the closure(time: on video, 12:55)? thanks

Hi! Glad you are enjoying it.

Not sure I understand your question but let me take a guess. (Apologies if I missed in advance. :wink: )

@escaping only applies to closures so you do not need it in the case of print(galileo.name) However if you wanted to wrap the Dispatch in a function and pass a closure into that, it would need to escape. Example:

func performAction(action: @escaping ()->Void) {
  DispatchQueue.global().asyncAfter(deadline: .now() + 0.1 ) {
    action()
  }
}

let galileo = Supernova("Galileo")
performAction {
  galileo.explode()
  print(galileo.name)
}

Anyway, that might not be what your asking but maybe it helps?

Hi @rayfix , just a small change needed: The challenge starter and final playgrounds are swapped!

I am not sure why your @ mention didn’t notify me but I will look into that. Thanks!

Hey Ray, i am confused about the reason as to why after we assign weak reference semantics to galieleo, we make it a strong reference using guard in the next line.

Hi @rayfix,

Thanks for the very informative video series. I have just a small correction to the slides for this video. The vertical text on the right side of slides 5 and 7 declares the increment function to be of the type (Int) -> Int, but the code in the box on the left declares it to be of type as (Int) -> Void, which is how the function is actually defined.