title: Overriding a superclass delegate in Swift
body: <p>I'm working on a Swift (v1.2) project which contains two UIViews. <em>MyView</em>, and <em>MyViewSubclass</em> . </p>
  
  <p><em>MyView</em> has a delegate that I want to override in <em>MyViewSubclass</em> to be a sub-protocol, similar to how <em>UITableViews</em> have a <em>UITableViewDelegate</em> which also conforms to the super <em>UIScrollViewDelegate</em>.</p>
  
  <p>My first idea was to override the superclass property, but this <strong>causes a compiler error</strong>, as a subclass can't override a superclass property with a different type.</p>
  
  <pre><code>// Example throws a compiler error. Can't override property with different type
  
  class MyView : UIView {
      weak var delegate : MyViewDelegate?
  }
  
  class MyViewSubclass : MyView {
      override weak var delegate : MyViewSubclassDelegate? // Compiler error
  }
  
  protocol MyViewDelegate {
     func someFunc ()
  }
  
  protocol MyViewSubclassDelegate : MyViewDelegate {
      //func someFunc () Edit: redefinition not necessary, thanks @Rob!
      func someOtherFunc ()
  }
  </code></pre>
  
  <p>My second idea was to override the implicit getter and setter methods of the delegate property. The subclass could have a getter which takes a <em>MyViewSubclassDelegate</em>, and casts down to the <em>MyViewDelegate</em> protocol, so the property itself won't need to be overridden.</p>
  
  <p>This method <strong>also results in a compiler error</strong>. The accessors can't actually "override" the superclass methods as they have different method signatures, and can't just be declared because their names conflict with the superclass setters.</p>
  
  <pre><code>// Example throws a compiler error. Can't declare function with a conflicting name
  
  class MyViewSubclass : MyView {
      func setDelegate ( newValue : MyViewSubclassDelegate ) { // Compiler error
          super.delegate = newValue as? MyViewDelegate
      }
  
      func getDelegate () -&gt; MyViewSubclassDelegate { // Compiler error
          return super.delegate as? MyViewSubclassDelegate
      }
  }
  </code></pre>
  
  <p>I could have the subclass override the getter and setter for the superclass, and just store the delegate in a separate subclass property, <strong>which does work</strong>, but then the subclass will look like it should be assigned a <em>MyViewDelegate</em>, when it really needs to be assigned a <em>MyViewSubclassDelegate</em>, which could cause serious confusion in the future.</p>
  
  <pre><code>// Example works, but it is unclear that MyViewSubclass's delegate should be 
  // assigned a MyViewSubclassDelegate
  
  class MyViewSubclass : MyView {
      private weak var _subclassDelegate : MyViewSubclassDelegate?
  
      override weak var delegate : MyViewDelegate? { // Ambiguous type
          didSet {
              _subclassDelegate = delegate as? MyViewSubclassDelegate
          }
      }
  }
  </code></pre>
  
  <p>I know something at least similar is possible, because classes like UITableView, and UICollectionView seem to do what I want, but they are also written in Objective-C rather than Swift, so the specifics of what the language allows may be different.</p>
  
  <p><strong>Is there any way to have a Swift subclass override a superclass's property with a sub-type?</strong></p>