title: Swift: Is there a way to let only certain subclasses have access to an extension of the superclass
body: <p>I am currently at the start of a project and trying to make some universal inspectable/designable variables for UIViews, UIButtons, and UIImageViews to make designing in InterfaceBuilder easier. For example:</p>
  
  <pre><code>@IBInspectable
  var cornerRadius: CGFloat {
      get {
          return layer.cornerRadius
      }
      set {
          layer.cornerRadius = newValue
      }
  }
  </code></pre>
  
  <p>One suggestion I've seen to accomplish this is:</p>
  
  <pre><code>extension UIView { 
  
      @IBInspectable
      var cornerRadius: CGFloat {
          get {
              return layer.cornerRadius
          }
          set {
              layer.cornerRadius = newValue
          }
      }
  
  }
  </code></pre>
  
  <p>However, the problem with this is that now <em>all</em> subclasses of UIView, show the inspectable properties, which is problematic when I will only be using these properties on maybe three classes, and I add more properties like border width/color, shadow radius, opacity, offset, and color, and these take up a lot of the inspector pane in IB.</p>
  
  <p>I'd like something like this:</p>
  
  <pre><code>@IBDesignable class MyView: UIView, MyDesignable {}
  @IBDesignable class MyButton: UIButton, MyDesignable {}
  @IBDesignable class MyImageView: UIImageView, MyDesignable {}
  
  protocol MyDesignable {}
  
  extension MyDesignable where Self: UIView {
  
      @IBInspectable
      var cornerRadius: CGFloat {
          get {
              return layer.cornerRadius
          }
          set {
              layer.cornerRadius = newValue
          }
      }
  
      ...
  
  </code></pre>
  
  <p>Unfortunately, <code>@IBInspectable</code> springs the error <code>Only instance properties can be declared @IBInspectable</code>.</p>
  
  <p>I also tried:</p>
  
  <pre><code>@IBDesignable class MyView: UIView {}
  @IBDesignable class MyButton: UIButton {}
  @IBDesignable class MyImageView: UIImageView {}
  
  private extension UIView {
  
      @IBInspectable
      var cornerRadius: CGFloat {
          get {
              return layer.cornerRadius
          }
          set {
              layer.cornerRadius = newValue
          }
      }
  
      ...
  }
  
  </code></pre>
  
  <p>However, other UIView subclasses still showed the inspectable properties.</p>
  
  <p>Hopefully the question and intent behind it make sense. Please let me know if you have any suggestions. Thank you!</p>