我试图快速了解仿制药在做什么。
我创建了这个样本游乐场
import UIKit public protocol MainControllerToModelInterface : class { func addGoal() init() } public protocol MainViewControllerInterface : class { associatedtype MODELVIEW var modelView: MODELVIEW? {get set} init(modelView: MODELVIEW) } public class MainViewController<M> : UIViewController, MainViewControllerInterface where M : MainControllerToModelInterface { public weak var modelView: M? required public init(modelView: M) { self.modelView = modelView super.init(nibName: String(describing: MainViewController.self), bundle: Bundle.main) } required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } public class Other<C, M> : NSObject where C : MainViewControllerInterface, C : UIViewController, M : MainControllerToModelInterface, C.MODELVIEW == M { var c : C? override init() { let m = M() self.c = C(modelView: m) super.init() } }
该行self.c = C(modelView: m)给我这个错误non-nominal type 'C' does not support explicit initialization
self.c = C(modelView: m)
non-nominal type 'C' does not support explicit initialization
从另一个堆栈溢出问题中,我看到较早的Xcode版本中的此错误意味着
cannot invoke initializer for type '%type' with an argument list of type '...' expected an argument list of type '...'
但是在上面的操场上,缺少什么编译器?
我在swift4 / xcode9上。
更新资料
遵循建议后Use C.init(modelView: m) rather than C(modelView: m),错误发生变化:
Use C.init(modelView: m) rather than C(modelView: m)
No 'C.Type.init' candidates produce the expected contextual result type '_?'
比@ vini- app建议删除UIViewController以使其工作。我仍然不明白为什么UIViewController在那里时编译器不满意。仅仅知道C具有有效的init方法还不够吗?
您只需要init在初始化通用参数而不是“真实”类型时显式使用:
init
self.c = C.init(modelView: m)