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?
extension Foo where
... can only be used ifFoo
isThe above error implies that
UIView
is a non-generic class type, and sowhere
clause cannot be applied here.So, rather than attempting to extend
UIView
, you must extendSomeProtocol
with a default implementation for cases whereSelf: UIView
Your extension should extend functionality to all types that conform to
SomeProtocol
and are of typeUIView
In a nutshell, you should switch the order in the extension clause. So,
should be