Generic IBDesginables UIView extension

2019-04-16 01:34发布

I would like to create generic extension for class to add some designables functionality to any UIView subclass and by this avoid adding functionality to all subclasses. So, would be nice to add extension for UIView which conforms to protocol SomeProtocol (It's empty because it is just a tag to mark classes which I want functionality to be added). Then just add that protocol in any UIView subclass where I want that functionality to be implemented like this:

protocol SomeProtocol {
  //empty
}

class BCBorderedView : UIView, SomeProtocol
{
  //empty
}

class BCBorderedSearch: UISearchBar, SomeProtocol
{
  //empty
}

My implementation below receives error with message

"Trailing 'where' clause for extension of non-generic type 'UIView'"

@IBDesignable
extension UIView where Self:SomeProtocol //Error: Trailing 'where' clause for extension of non-generic type 'UIView'
{

  @IBInspectable
  public var cornerRadius: CGFloat
  {
      set (radius) {
          self.layer.cornerRadius = radius
          self.layer.masksToBounds = radius > 0
      }

      get {
          return self.layer.cornerRadius
      }
  }

  @IBInspectable
  public var borderWidth: CGFloat
  {
      set (borderWidth) {
          self.layer.borderWidth = borderWidth
      }

      get {
          return self.layer.borderWidth
      }
  }

  @IBInspectable
  public var borderColor:UIColor?
  {
    set (color) {
        self.layer.borderColor = color?.cgColor
    }

    get {
        if let color = self.layer.borderColor
        {
            return UIColor(cgColor: color)
        } else {
            return nil
        }
    }
  }
}

Removing where clause compiles and works, but it adds functionality to all UIViews and it's subclasses (basically all UI elements) which make IB agent crash from time to time in storyboard.

Any ideas how this plan can be updated to work?

1条回答
我想做一个坏孩纸
2楼-- · 2019-04-16 01:38

extension Foo where ... can only be used if Foo is

  • a generic class or structure
  • a protocol containing some associated types
  • a protocol where we extend with a default implementation for when Self is of a specific (object/reference) type, or conforms to some type constraint.

Trailing 'where' clause for extension of non-generic type 'UIView'

The above error implies that UIView is a non-generic class type, and so where clause cannot be applied here.

So, rather than attempting to extend UIView, you must extend SomeProtocol with a default implementation for cases where Self: UIView

Your extension should extend functionality to all types that conform to SomeProtocol and are of type UIView

In a nutshell, you should switch the order in the extension clause. So,

extension UIView where Self : SomeProtocol

should be

extension SomeProtocol where Self: UIView
查看更多
登录 后发表回答