【问题标题】:How to use generics as params?(Swift 2.0)如何使用泛型作为参数?(Swift 2.0)
【发布时间】:2015-11-15 07:28:03
【问题描述】:

游乐场代码在这里

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)行内

得到编译错误:

无法将“[ProductModel]”类型的值转换为预期参数 输入“[_]”。

我的问题是如何在GenericListProtocol 中使用 setData 方法?

任何人都可以提供帮助,我们将不胜感激。

【问题讨论】:

    标签: ios swift generics swift2


    【解决方案1】:

    ProductModel 类型移动到扩展中并从通用协议中删除约束似乎可行。

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

    【讨论】:

    • 乐于助人。这是一个很好的例子。
    【解决方案2】:

    我发现这个问题很有趣,并想如何以通用的方式解决它。

    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)
    

    感谢您的想法和建议,如果可以进一步改进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      相关资源
      最近更新 更多