title: Type constraint on AnyObject or Any (AnyObject or Any that conforms to a protocol)
body: <p>I'm trying to convert my Objective-C code to swift.
  In Objective-C I have the following protocol:</p>
  
  <pre><code>@protocol RWOverlaySelectionDelegate &lt;NSObject&gt;
      -(void)areaSelected:(UIView *)view allPoints:(NSArray *)points;
  @end
  </code></pre>
  
  <p>and following class has a weak property that reference the protocol (obviously it's defined as weak to prevent strong reference cycle).</p>
  
  <pre><code>@interface RWMapSelectionLayer : UIView
      @property(weak, nonatomic) id &lt;RWOverlaySelectionDelegate&gt; delegate;
  @end
  </code></pre>
  
  <p>Now the Swift equivalent:</p>
  
  <p>the protocol:</p>
  
  <pre><code>protocol RWOverlaySelectionDelegate {
      func areaSelected(view:UIView,points:CGPoint[])
  }
  </code></pre>
  
  <p>and the class that has a property which conforms to that protocol:</p>
  
  <pre><code>class RWMapSelectionLayer:NSObject {
      weak var delegate:RWOverlaySelectionDelegate?
  }
  </code></pre>
  
  <p>But I'm getting <code>'weak' cannot be applied to non-class type 'RWOverlaySelectionDelegate'</code> compile time error in this line : <code>weak var delegate:RWOverlaySelectionDelegate?</code></p>
  
  <p>Then I tried to convert my property to an <code>AnyObject</code> that conforms to <code>RWOverlaySelectionDelegate</code> using following syntax:</p>
  
  <pre><code>weak var delegate: AnyObject&lt;RWOverlaySelectionDelegate&gt;?
  </code></pre>
  
  <p>Now I'm interfering with Generics and compiler showing: <code>Cannot specialize non-generic type 'AnyObject'</code> error.</p>
  
  <p>In another unsuccessful try I changed it to </p>
  
  <pre><code>weak var delegate: AnyObject:RWOverlaySelectionDelegate?
  </code></pre>
  
  <p>read it as "Delegate is on type AnyObject where AnyObject should conform to RWOverlaySelectionDelegate"</p>
  
  <p>This is again incorrect because of two colons(:) in a single statement.</p>
  
  <p>Would appreciate if someone can help me to <code>enforce conformance to a protocol on AnyObject or Any</code></p>