title: Proxy Pattern in iOS - Swift
body: <p>I need to create proxy pattern in iOS using swift</p>
  
  <p>I have tried it using Objective C and here is the code</p>
  
  <p><strong>MyProtocol.h</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  @protocol MyProtocol &lt;NSObject&gt;
  @required
  -(void)testMessage;    
  @end
  </code></pre>
  
  <p><strong>TestBO.h</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "MyProtocol.h"
  
  @interface TestBO : NSObject &lt;MyProtocol&gt;
  
  @end
  </code></pre>
  
  <p><strong>TestBO.m</strong></p>
  
  <pre><code>#import "TestBO.h"
  
  @implementation TestBO 
  
  -(void)testMessage{
      NSLog(@"Test Message");
  }
  
  @end
  </code></pre>
  
  <p><strong>TestProxyHandler.h</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface TestProxyHandler : NSProxy
  
  @property (nonatomic, strong) id object;
  
  - (instancetype)initWithProtocol:(Protocol *)protocol andObject:(Class)clazz;
  
  - (void)forwardInvocation:(NSInvocation *)invocation;
  
  - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector;
  
  @end
  </code></pre>
  
  <p><strong>TestProxyHandler.m</strong></p>
  
  <pre><code>#import "TestProxyHandler.h"
  #import "TestBO.h"
  
  @implementation TestProxyHandler 
  
  - (instancetype)initWithProtocol:(Protocol *)protocol andObject:(Class)clazz{
      if ([clazz conformsToProtocol:@protocol(MyProtocol)]) {
          self.object = [[clazz alloc] init];
      }else{
          NSLog(@"Error it does not conform to protocol");
      }
      return self;
  }
  
  - (void)forwardInvocation:(NSInvocation *)invocation{
      NSString *selString = NSStringFromSelector(invocation.selector);
      NSLog(@"Called %@",selString);
      [invocation invokeWithTarget:self.object];
  }
  
  - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
      return [self.object methodSignatureForSelector:selector];
  }
  
  
  @end
  </code></pre>
  
  <p>I have invoked it using</p>
  
  <pre><code>id &lt;MyProtocol&gt; delegate = (TestBO *)[[TestProxyHandler alloc] initWithProtocol:@protocol(MyProtocol) andObject:[TestBO class]];
  
  [delegate testMessage];
  </code></pre>
  
  <p>But I am not able to make it work in Swift even the initialzier is showing that the message </p>
  
  <p><strong>TestHandler.swift</strong></p>
  
  <pre><code>import Foundation
  class TestHandler: NSProxy {
      var object: AnyObject
  
      convenience override init(`protocol`: Protocol, andObject clazz: AnyClass) {
          if clazz.conformsToProtocol() {
              self.object = clazz()
          }
          else {
              NSLog("Error it does not conform to protocol")
          }
      }        
  }
  </code></pre>
  
  <p>Does anyone have any clue to do this in swift ??</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>In <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Proxy.html" rel="noreferrer">java</a> you can create runtime implementation of a method using the <strong>Proxy.newProxyInstance</strong> call but can this be achieved in iOS ? using swift ? Any clue ?</p>