简体   繁体   English

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

[英]Swift - passing parameter from non-generic function to a generic one

So here is my playground, i would love to use the openInitial111() function but it just wont compile, the openInitial222() is working perfectly, whats the problem ? 所以这是我的游乐场,我很想使用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")
    }
}

Indeed as suggested in comments the underlaying issue is that protocol doesn't conform to protocol. 实际上,正如评论中所建议的那样,潜在的问题是协议不符合协议。 The error message is incredibly misleading though. 错误消息是令人难以置信的误导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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