泛型IBDesginables UIView扩展 [英] Generic IBDesginables UIView extension
问题描述
我想为类创建泛型扩展,以便为任何UIView子类添加一些可设计的功能,从而避免为所有子类添加功能。所以,为符合协议的强大的UIView添加扩展会很好,因为它只是一个标记我希望添加功能的类的标记。然后,只需在任何UIView子类中添加该协议,并在其中实现此功能即可:
protocol SomeProtocol {
//空
}
类BCBorderedView:UIView,SomeProtocol
{
//空
}
类BCBorderedSearch :UISearchBar,SomeProtocol
{
//空
}
我的下面的实现接收错误消息
Trailing'where'子句用于扩展非泛型类型'UIView'
@IBDesignable
扩展名UIView其中Self:SomeProtocol //错误:尾随'where'子句for扩展非泛型类型'UIView'
{
@IBInspectable
public var cornerRadius:CGFloat
{
set(radius){
self.layer.cornerRadius =半径
self.layer.masksToBounds =半径> 0
}
获得{
返回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
}
}
}
}
删除其中子句编译和工作,但它为所有UIViews及其子类(基本上所有UI元素)添加了功能,这些功能使IB代理不时在故事板中崩溃。
任何想法如何将此计划更新为work?
尾随'where'子句对于非泛型类型'UIView'的扩展 上面的错误意味着 比尝试扩展 您的扩展应该扩展到符合 简而言之,您应该在扩展子句中切换顺序。所以, 应该是 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: My implementation below receives error with message "Trailing 'where' clause for extension of non-generic type 'UIView'"
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其中
...只能使用如果 Foo
是
Self是特定的(对象/参考e)类型,或者符合一些
类型的约束。
lock
UIView
是一个非泛型的类类型,所以其中
子句不能在这里应用。
UIView
,您必须扩展 SomeProtocol
,其中 Self :UIView
SomeProtocol
的所有类型的功能,是 UIView
扩展UIView其中Self:SomeProtocol
扩展SomeProtocol其中Self:UIView
protocol SomeProtocol {
//empty
}
class BCBorderedView : UIView, SomeProtocol
{
//empty
}
class BCBorderedSearch: UISearchBar, SomeProtocol
{
//empty
}
@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
}
}
}
}
extension Foo where
... can only be used if Foo
is
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
这篇关于泛型IBDesginables UIView扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!