简体   繁体   中英

How to use generics as params?(Swift 2.0)

Code in playground is here

class ProductModel {
    var productID : Int = 0
    init(id:Int) {
        productID = id
    }
}


protocol GenericListProtocol {
    typealias T = ProductModel
    var list : [T] { get set }
    var filteredlist : [T] { get set }
    func setData(list : [T])
}
extension GenericListProtocol {
    func setData(list: [T]) {
        list.forEach { item in
            guard let productItem = item as? ProductModel else {
                return
            }
            print(productItem.productID)
        }
    }
}

class testProtocol {
    class func myfunc<N:GenericListProtocol>(re:N){
        var list : [ProductModel] = [ProductModel(id: 1),ProductModel(id: 2),ProductModel(id: 3),ProductModel(id: 4)]
        re.setData(list)
    }
}

But in the line re.setData(list)

get compile error:

Cannot convert value of type '[ProductModel]' to expected argument type '[_]'.

My Question is How to use setData method in GenericListProtocol ?

Anyone could help will be appreciated.

Moving the ProductModel type into the extension and removing the constraint from the generic protocol seems to work.

class ProductModel {
    var productID : Int = 0
    init(id:Int) {
        productID = id
    }
}

protocol GenericListProtocol {
    typealias T
    var list : [T] { get set }
    var filteredlist : [T] { get set }
    func setData(list : [T])
}

extension GenericListProtocol {
    func setData(list: [ProductModel]) {
        list.forEach { item in
            print(item.productID)
        }
    }
}

class testProtocol {
    class func myfunc<N:GenericListProtocol>(re:N) {
        let list : [ProductModel] = [ProductModel(id: 1),ProductModel(id: 2),ProductModel(id: 3),ProductModel(id: 4)]
        re.setData(list)
    }
}

I found this question interesting and thought how best we could solve it in generic way.

protocol Product {
    var productID : Int {get set}
}

class ProductModel: Product {
    var productID : Int = 0
    init(id:Int) {
        productID = id
    }
}

protocol GenericListProtocol {
    typealias T : Product
    var list : [T] { get set }
    var filteredlist : [T] { get set }

}

extension GenericListProtocol {
    func setData(list: [T]) {
        list.forEach { item in
            print(item.productID)
        }
    }
}

class GenericListProtocolClass : GenericListProtocol
{
    typealias T = ProductModel
    var intVal = 0

    var list =  [T]()
    var filteredlist = [T]()

}

class testProtocol {
    class func myfunc(re: GenericListProtocolClass){
        let list : [ProductModel] = [ProductModel(id: 1),ProductModel(id: 2),ProductModel(id: 3),ProductModel(id: 4)]
        re.setData(list)
    }
}


let temp = GenericListProtocolClass()
testProtocol.myfunc(temp)

Appreciate your thought and suggestion if it can be improved further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM