I defined protocol:
我定义了协议:
protocol MyProtocol: class {
}
class MyClass: MyProtocol {
}
And extension only for collections with class elements:
并且只对具有类元素的集合进行扩展:
extension CollectionType where Generator.Element: class {
func someFunc() {
}
}
var items = [MyClass]()
items.someFunc() // someFunc is available
var strings = [String]()
// strings.someFunc() is not available because String is not class
But this does not compile and I changed extension definition to:
但这不编译,我将扩展定义更改为:
extension CollectionType where Generator.Element: AnyObject {
...
}
It also does not compile because MyProtocol
does not conform to AnyObject
. So I changed definition of MyProtocol
to:
它也不会编译,因为MyProtocol不符合AnyObject。所以我将MyProtocol的定义更改为:
protocol MyProtocol: AnyObject {
}
It also does not compile with message:
它也不会与消息编译:
Type 'MyProtocol' does not conform to protocol 'AnyObject'.
类型'MyProtocol'不符合协议'AnyObject'。
1 个解决方案
#1
0
After a little research I found here that protocol type can not conform to protocol:
经过一番研究后我发现协议类型不符合协议:
extension CollectionType where Generator.Element : First {
func someFunc() {
}
}
protocol First {
}
protocol Second: First {
}
var items = [Second]()
items.someFunc() // Type 'Second' does not conform to protocol 'First'
So the real issue in my case is not that class-only protocol does not satisfy AnyObject
constraint but that protocol type can not conform AnyObject
protocol or any other protocol.
因此,在我的情况下,真正的问题不是仅类协议不满足AnyObject约束,而是协议类型不能符合AnyObject协议或任何其他协议。
#1
0
After a little research I found here that protocol type can not conform to protocol:
经过一番研究后我发现协议类型不符合协议:
extension CollectionType where Generator.Element : First {
func someFunc() {
}
}
protocol First {
}
protocol Second: First {
}
var items = [Second]()
items.someFunc() // Type 'Second' does not conform to protocol 'First'
So the real issue in my case is not that class-only protocol does not satisfy AnyObject
constraint but that protocol type can not conform AnyObject
protocol or any other protocol.
因此,在我的情况下,真正的问题不是仅类协议不满足AnyObject约束,而是协议类型不能符合AnyObject协议或任何其他协议。