Forke
Forke

Reputation: 346

Type "MyClass" does not conform to protocol "Collection_Delegate"

I have a class MyClass implementing a generic function of a delegate Collection_Delegate.

My classes Collection and Item are superclasses for some specific classes

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
    }
 }

Now in some subclasses of Collection I want to call the generic onFetAllCompleted function of the delegate avery subclass has. But the class implementing the Collection_Delegate protocol causing compiler errors

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()
    }
}

Here we go. The class **MyClass* gets the error

Type "MyClass" does not conform to protocol "Collection_Delegate"

Within the generic function I get the error

'U' is not convertible to 'Item'

So what am I doing wrong? Why doesn't the generic stuff work?

Upvotes: 1

Views: 115

Answers (1)

Zell B.
Zell B.

Reputation: 10296

I think you over complicated things a bit with your generic function declaration. If I understood you correctly your onFetchAllCompleted function takes parameter T which is Collection of U's and U is an Item. If that is correct the above expression can be simplified like this : onFetchAllCompleted function takes parameter T which is Collection of Items. So your protocol and class should look like this

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()
    }
}

Let me know if this helped you

Upvotes: 1

Related Questions