代碼在操場這裏如何使用仿製藥PARAMS(雨燕2.0)
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)
}
}
但在該行re.setData(list)
得到編譯錯誤:
Cannot convert value of type '[ProductModel]' to expected argument type '[_]'.
我的問題是如何在GenericListProtocol
中使用setData方法?
任何人都可以幫助將不勝感激。
謝謝,我真的更新與泛型.. –
樂於幫助。這是一個很好的例子。 – Daniel