title: Swift - how to implement protocol methods in function
body: <p>In android, it is possible to implement interface methods during function call. For example, the declaration of interface and function:</p>
  
  <pre><code>//interface
  interface exampleCallback {
      void success(String response);
      void error(String error);
  }
  
  //function
  void exampleFunction(exampleCallback cb) {
      //do something
  }
  </code></pre>
  
  <p>And the function call:</p>
  
  <pre><code>exampleFunction(new exampleCallback() {
      @Override
      public void success(String response) {
          //custom method implementation
      }
      @Override
      public void error(String error) {
          //custom method implementation
      }
  });
  </code></pre>
  
  <p>That is, the success/error methods can be customized for each function call of the exampleFunction.
  However, after some google search, I can only find example codes for implementing protocol methods in class or struct declaration, in which the methods can no longer be customized. I know I can pass escaping closure as function parameter to achieve the goal, i.e., customize callback function for each function call. But just wonder if I can use protocol to do the similar thing...</p>