Swift Delegate for a Generic Class

Swift delegate for a generic class

It is hard to know what the best solution is to your problem without having more information, but one possible solution is to change your protocol declaration to this:

protocol MyClassDelegate: class {
func valueChanged<T>(genericClass: MyClass<T>)
}

That removes the need for a typealias in the protocol and should resolve the error messages that you've been getting.

Part of the reason why I'm not sure if this is the best solution for you is because I don't know how or where the valueChanged function is called, and so I don't know if it is practical to add a generic parameter to that function. If this solution doesn't work, post a comment.

Using delegates on generic protocol

This is not possible to use generic protocols. Use concrete type or usual protocol instead. You have to declare concrete T outside of those abstractions

For instance, this should work:

class MyType<T> {

weak var delegate: UsingGenericProtocol<T>? // First error

var t: T

init(t: T) {
self.t = t
}

func finished() {
delegate?.funca(component: t) // Second error
}

}
class UsingGenericProtocol<T>: GenericProtocol {
let myType: MyType<T>
typealias type = T

init(t: T) {
myType = MyType<T>(t: t)
}

func funca(component: T) {

}
}

let instance = UsingGenericProtocol<Int>(t: 0)

Set a generic class as a delegate in swift

When my XMLUtil class is a generic class

But Objective-C knows nothing of generic classes. So there is no way to show your XMLUtil class to Objective-C. Thus, it cannot serve as NSXMLParser's delegate; NSXMLParser is an Objective-C class and cannot see your XMLUtil class if it is generic.

One easy way to see this is to try to mark your XMLUtil class as @objc. You will fail; the compiler will stop you. There is no way to show this class to Objective-C.

Using associatedtype in a delegate protocol for a generic type

You could remove the associated type requirement from your protocol and use a generic function game instead:

protocol GamePointsDelegate {
func game<B>(_ game: Game<B>, didSetPoints points: Int)
}

So you can use the code of your Game class as it is but the downside is that the class which conforms to the protocol has to handle all Boards.

Generic delegate in Swift

Generic functions provide single template implementation for multiple types referred by T, while what it seems you are trying to do is to provide an implementation of fetchItemsForPage method specifically for Car type.

The way it would work is:

protocol DataAPIDelegate: class {
func fetchItemsForPage<T>(api: DataAPI<T>, page: Int, onFinish: ([T] -> ()))
}

class DataAPI<T> {
weak var delegate: DataAPIDelegate? = nil

//...
//some other methods
}

struct Car { }

class CarsViewController: DataAPIDelegate {
var dataAPI = DataAPI<Car>()

// MARK :- DataAPIDelegate
func fetchItemsForPage<T>(api: DataAPI<T>, page: Int, onFinish: ([T] -> ())) {
let items: [T] = [T]()
onFinish(items)
}
}

... while what you are trying to do is:

struct Bike { }

class BikesViewController: DataAPIDelegate {
var dataAPI = DataAPI<Bike>()

// MARK :- DataAPIDelegate
func fetchItemsForPage<Bike>(api: DataAPI<Bike>, page: Int, onFinish: ([Bike] -> ())) {
let items: [Bike] = [Bike]()
onFinish(items)
}
}

... but in the latter snippet Bike is not a type but a placeholder, which works just the same as if you would use T.



Related Topics



Leave a reply



Submit