Ios 特定泛型类型参数的变通方法

Ios 特定泛型类型参数的变通方法,ios,swift,generics,Ios,Swift,Generics,情况: 我有两个协议,一个是静态方法: protocol DataSourceable { static func getMoreData<T: DataAccepting>(someObject: T) } protocol DataAccepting { func accept(data: [Any]) } extension DataAccepting where Self: UIViewController { } 最终目标是能够做到这一点: class

情况:

我有两个协议,一个是静态方法:

protocol DataSourceable {
    static func getMoreData<T: DataAccepting>(someObject: T)
}

protocol DataAccepting {
    func accept(data: [Any])
}
extension DataAccepting where Self: UIViewController { }
最终目标是能够做到这一点:

class SampleViewController<T: DataSourceable>: UIViewController, DataAccepting {
var intArray = [Int]()
    func setup() {
        T.getMoreData(dataAcceptor: self)
    }

    func accept(data: [Any]) {
        intArray = data
    }
}

struct SampleModel: DataSourceable {
    static func getMoreData<T: DataAccepting>(dataAcceptor: T) {
        var anIntArray = [Int]()
        someObject.accept(anIntArray)
    }
}
class SampleViewController:UIViewController,数据接受{
var intArray=[Int]()
函数设置(){
T.getMoreData(数据接受器:self)
}
func接受(数据:[任何]){
intArray=数据
}
}
结构示例模型:可数据源{
静态函数getMoreData(数据接受器:T){
变量anIntArray=[Int]()
someObject.accept(一个tarray)
}
}
然后制作一个
SampleViewController

这将允许我让SampleModel处理控制器的源数据。SampleModel决定如何获取数据,然后使用控制器上的accept()函数将数据提供给SampleController。

这似乎是一个编译器错误。但是,正如我们在中讨论的那样,您通常应该避免这种设计。

哦,该死。=/看到有效的Swift导致编译器错误总是很糟糕。不过,如果要将self作为参数传递,为什么首先要将其设为静态方法?您只需使用一个参数将其设置为实例方法,然后调用
self.someProtocolMethod(num:0)
@AlexanderMomchliov-Hmm,我不确定这是否会实现相同的行为。基本上,符合
ProcotolA
的每种类型都需要一个函数,该函数可以接受
Int
和泛型类型
T
的对象。让我编辑一下我的问题,让它更复杂一点;)。
someProtocolMethod()
中的类型
T
也有一个约束。这个示例对我来说太抽象了,无法理解您要做的事情。你能详细解释一下吗?@AlexanderMomchliov刚刚添加了一些更具描述性的名称和细节,这更有意义吗?
class SampleViewController<T: DataSourceable>: UIViewController, DataAccepting {
var intArray = [Int]()
    func setup() {
        T.getMoreData(dataAcceptor: self)
    }

    func accept(data: [Any]) {
        intArray = data
    }
}

struct SampleModel: DataSourceable {
    static func getMoreData<T: DataAccepting>(dataAcceptor: T) {
        var anIntArray = [Int]()
        someObject.accept(anIntArray)
    }
}