非名义类型X不支持显式初始化 [英] non-nominal type X does not support explicit initialization
问题描述
我创建了这个示例游乐场
import UIKit
公共协议MainControllerToModelInterface:class {
func addGoal()
init()
}
公共协议MainViewControllerInterface:class {
associatedtype MODELVIEW
var modelView:MODELVIEW? {get set}
init(modelView:MODELVIEW)
}
public class MainViewController< M> :UIViewController,MainViewControllerInterface其中M:MainControllerToModelInterface {
public weak var modelView:M?
需要public init(modelView:M){
self.modelView = modelView
super.init(nibName:String(描述:MainViewController.self),bundle:Bundle.main )
需要public init?(coder aDecoder:NSCoder){
fatalError(init(coder :) has been implemented now)
}
}
公开课其他< C,M> :NSObject其中C:MainViewControllerInterface,C:UIViewController,M:MainControllerToModelInterface,C.MODELVIEW == M {
var c:C?
覆盖init(){
let m = M()
self.c = C(modelView:m)
super.init()
}
行 self.c = C(modelView :m)
给我这个错误非名义类型'C'不支持显式初始化
从这个其他堆栈溢出问题我发现旧版Xcode版本中的这个错误意味着
$ b $ p $ 不能用类型''的参数列表调用类型'%type'的初始值设定项。 ..'期望的类型为'...'的参数列表
但是在操作上面什么是编译器缺失?
我在swift4 / xcode9上。
更新 b
遵循建议使用C.init(modelView:m)而不是C(modelView:m)
错误更改:
否'C.Type.init'候选产生预期的上下文结果类型'_?'
比@ vini-app建议移除UIViewController以使其工作。我仍然不明白为什么当UIViewController存在时编译器不开心。是否仅仅知道C有这个有效的init方法是不够的?
您只需要使用
自己初始化一个泛型参数时显式初始化
.c = C.init(modelView:m)
I'm trying to understand what I'm doing wrong with generics in swift.
I created this sample playground
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()
}
}
the line self.c = C(modelView: m)
gives me this error non-nominal type 'C' does not support explicit initialization
From this other stack overflow question I see that this error in older Xcode versions means
cannot invoke initializer for type '%type' with an argument list of type '...' expected an argument list of type '...'
But in the playground above what is the compiler missing?
I'm on swift4/xcode9.
Update
After following the suggestion Use C.init(modelView: m) rather than C(modelView: m)
the error changes in:
No 'C.Type.init' candidates produce the expected contextual result type '_?'
Than @vini-app suggested to remove the UIViewController to make it works. By I still don't understand why the compiler is not happy when UIViewController is there. Is it not enough to know that C has that valid init method?
You just need to use init
explicitly whenever you're initializing a generic parameter rather than a "real" type:
self.c = C.init(modelView: m)
这篇关于非名义类型X不支持显式初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!