title: Swift @escaping and Completion Handler
body: <p>I am trying to understand 'Closure' of Swift more precisely.</p>
  
  <p>But <code>@escaping</code> and <code>Completion Handler</code> are too difficult to understand</p>
  
  <p>I searched many Swift postings and official documents, but I felt it was still not enough.</p>
  
  <p>This is the code example of official documents </p>
  
  <pre><code>var completionHandlers: [()-&gt;Void] = []
  
  func someFunctionWithEscapingClosure(completionHandler: @escaping ()-&gt;Void){
      completionHandlers.append(completionHandler)
  }
  
  func someFunctionWithNoneescapingClosure(closure: ()-&gt;Void){
      closure()
  }
  
  class SomeClass{
      var x:Int = 10
      func doSomething(){
          someFunctionWithEscapingClosure {
              self.x = 100
              //not excute yet
          }
          someFunctionWithNoneescapingClosure {
              x = 200
          }
      }
  }
  
  let instance = SomeClass()
  instance.doSomething()
  print(instance.x)
  
  completionHandlers.first?() 
  print(instance.x)
  </code></pre>
  
  <p>I heard that there are two ways and reasons using <code>@escaping</code> </p>
  
  <p>First is for storing a closure, second is for Async operating purposes.</p>
  
  <p><strong>The following are my questions</strong>:</p>
  
  <p>First, if <code>doSomething</code> executes then <code>someFunctionWithEscapingClosure</code> will executing with closure parameter and that closure will be saved in global variable array. </p>
  
  <p>I think that closure is {self.x = 100}</p>
  
  <p>How <code>self</code> in {self.x = 100} that saved in global variable <code>completionHandlers</code> can connect to <code>instance</code> that object of <code>SomeClass</code> ?</p>
  
  <p>Second, I understanding <code>someFunctionWithEscapingClosure</code> like this.</p>
  
  <p>To store local variable closure <code>completionHandler</code> to global variable 'completionHandlers<code>we using</code>@escaping` keyword! </p>
  
  <p>without <code>@escaping</code> keyword <code>someFunctionWithEscapingClosure</code> returns, local variable <code>completionHandler</code> will remove from memory</p>
  
  <p><code>@escaping</code> is keep that closure in the memory</p>
  
  <p>Is this right?</p>
  
  <p>Lastly, I just wonder about the existence of this grammar.</p>
  
  <p>Maybe this is a very rudimentary question.</p>
  
  <p>If we want some function to execute after some specific function. Why don't we just call some function after a specific function call?</p>
  
  <p>What are the differences between using the above pattern and using an escaping callback function? </p>