作者:手机用户2502902093 | 来源:互联网 | 2023-07-26 12:07
IneedtodeclareavariableoftypeUIViewwhichalsoconformstoMyProtocol:我需要声明一个UIView类型的变量,它也
I need to declare a variable of type UIView
which also conforms to MyProtocol
:
我需要声明一个UIView类型的变量,它也符合MyProtocol:
protocol MyProtocol: class {
func foobar()
}
class MyClass {
var myView: UIView! // Error: Cannot specialize non-generic type 'UIView'
}
However I get the compiler error: Cannot specialize non-generic type 'UIView'.
但是我得到了编译器错误:不能专门化非泛型类型'UIView'。
I need to access methods on the variable from UIView
and MyProtocol
.
我需要从UIView和MyProtocol访问变量的方法。
What is the correct variable declaration to support these requirements?
支持这些要求的正确变量声明是什么?
If it makes any difference, only UIView
subclasses will implement the protocol. Currently I add protocol conformance via extensions.
如果它有任何区别,只有UIView子类才会实现该协议。目前,我通过扩展添加协议一致性。
I found this answer: https://stackoverflow.com/a/25771265/233602 but it's not clear if that answer is still the best option going in when writing in Swift 2.
我找到了这个答案:https://stackoverflow.com/a/25771265/233602但不清楚在Swift 2中写这个答案是否仍然是最好的选择。
2 个解决方案