Swift - 将参数从非泛型函数传递给泛型函数

时间:2016-11-07 11:29:29

标签: ios swift generics swift3

所以这是我的游乐场,我很想使用openInitial111()函数,但它只是不会编译,openInitial222()工作正常,是什么问题?

protocol Module : Screen {
   init()
}
extension Module {
   static func entity() -> Self {
      return Self()
   }
}
protocol Screen {

}
class FFF: Module {
   required init() {
   }
}
class ZZZ: NSObject {
    func openInitial111(initialModule: Module.Type) {
        self.openViewController(itemWithScreenStyle: initialModule.entity())  // Error : Cannot invoke openViewController
                                                                          //with an argument list of type '(initWithScreenStyle:Module)
    }

    func openInitial222(initialModule: Module.Type) {
       let f = FFF.self  // FFF.Type
       self.openViewController(itemWithScreenStyle: f.entity())  // works
    }


    func openViewController<T: Screen>(itemWithScreenStyle: T) {
        print("generics")
    }
}

1 个答案:

答案 0 :(得分:0)

确实如评论中所建议的那样,问题是协议不符合协议。错误信息令人难以置信的误导。

相关问题