Ios “类型”;“我的班级”;不符合协议”的规定;集合“代表”;

Ios “类型”;“我的班级”;不符合协议”的规定;集合“代表”;,ios,swift,generics,Ios,Swift,Generics,我有一个类MyClass实现委托的通用函数Collection\u delegate 我的类集合和项目是某些特定类的超类 protocol Collection_Delegate { func onFetchAllCompleted<T, U where T: Collection<U>, U: Item>(collection: T, error: String?) } class Collection<T>: Item { pr

我有一个类MyClass实现委托的通用函数Collection\u delegate

我的类集合项目是某些特定类的超类

protocol Collection_Delegate {
        func onFetchAllCompleted<T, U where T: Collection<U>, U: Item>(collection: T, error: String?)
}

class Collection<T>: Item {

    private var items: [T]

    override init (communicator: CG_API_Communicator) {
        items = [T]()
        super.init(communicator: communicator)
    }

    internal func fetchAll() {
        fatalError(notImplemented)
    }

    internal func onFetchAllCompleted(error: String?, json: JSON?) {
        fatalError(notImplemented)
    }

    internal func appendItem(item: T) {
        self.items.append(item)
    }

    internal func getItems() -> [T] {
        return self.items
    }
}

class Item {

    var itemDataRaw: JSON?        

    func toString() -> String? {
        var retval: String?
        if let value: String = itemDataRaw?.rawString(encoding: NSUTF8StringEncoding) {
            retval = value
        } else {
            retval = "Something went badly wrong"
        }
        return retval
    }
 }
协议集合\u委托{
func onFetchAllCompleted(集合:T,错误:字符串?)
}
类集合:项目{
专用var项目:[T]
重写初始化(通信器:CG\U API\U通信器){
项目=[T]()
super.init(通信器:通信器)
}
内部函数fetchAll(){
fatalError(未实施)
}
内部函数onFetchAllCompleted(错误:String?,json:json?){
fatalError(未实施)
}
内部职能附录项目(项目:T){
self.items.append(项目)
}
内部函数getItems()->[T]{
退回自助物品
}
}
类项目{
var itemDataRaw:JSON?
func toString()->字符串{
var-retval:String?
如果let值:String=itemDataRaw?.rawString(编码:NSUTF8StringEncoding){
retval=值
}否则{
retval=“出了严重问题”
}
返回返回
}
}
现在在集合的一些子类中我想调用委托avery子类的泛型onfetalCompleted函数。但是实现集合\u委托协议的类会导致编译器错误

class MyClass: Collection_Delegate { // Error

    func onFetchAllCompleted<T, U where T: Collection<U>, U: Item>(collection: T, error: String?){
        println("MyClass.onFetchAllCompleted:\(_stdlib_getTypeName(collection))") // This displays the right class name of the subclasses
        let item: Item = collection.getItems()[0] //Error
        let string = item.toString()
    }
}
class MyClass:Collection\u委托{//错误
func onFetchAllCompleted(集合:T,错误:字符串?){
println(“MyClass.onFetchAllCompleted:\(\u stdlib\u getTypeName(collection))”//这将显示子类的正确类名
let item:item=collection.getItems()[0]//错误
let string=item.toString()
}
}
我们开始吧。类**MyClass*获取错误

类型“MyClass”不符合协议“Collection\u Delegate”

在泛型函数中,我得到了错误

“U”不能转换为“项目”


那么我做错了什么?为什么泛型的东西不起作用?

我认为你的泛型函数声明有点过于复杂了。如果我理解正确,那么onFetchAllCompleted函数接受参数T,它是U的集合,U是一个项。如果这是正确的,上面的表达式可以简化为:onFetchAllCompleted函数接受参数T,它是项的集合。因此,您的协议和类应该如下所示

protocol Collection_Delegate {
    func onFetchAllCompleted<T: Collection<Item>>(collection: T, error: String?)
}

class MyClass: Collection_Delegate {

    func onFetchAllCompleted<T: Collection<Item>>(collection: T, error: String?){
        println("MyClass.onFetchAllCompleted:\(_stdlib_getTypeName(collection))") // This displays the right class name of the subclasses
        let item: Item = collection.getItems()[0] //Error
        let string = item.toString()
    }
}
协议集合\u委托{
func onFetchAllCompleted(集合:T,错误:字符串?)
}
类MyClass:集合\u委托{
func onFetchAllCompleted(集合:T,错误:字符串?){
println(“MyClass.onFetchAllCompleted:\(\u stdlib\u getTypeName(collection))”//这将显示子类的正确类名
let item:item=collection.getItems()[0]//错误
let string=item.toString()
}
}

让我知道这是否有助于你

好吧,谢谢,这有助于我让东西正常工作。除非我遗漏了什么,否则也不需要
t
;您可以只完成
func onFetchAllCompleted(collection:collection,error:String?)
看起来我也错过了。你是绝对正确的事实上我让它“onFetchAllCompleted(collection:collection,error:String?)”来适应结构的其余部分,但它也有点像这个问题仍然是。。。为什么类不确认协议。它可能很复杂,但它适合函数的方法存根,并且没有语法错误。。。