xcode 非标称类型 X 不支持显式初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46458657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:19:30  来源:igfitidea点击:

non-nominal type X does not support explicit initialization

iosswiftxcodegenericsswift4

提问by Luca Bartoletti

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

该行self.c = C(modelView: m)给了我这个错误non-nominal type 'C' does not support explicit initialization

From this other stack overflowquestion I see that this error in older Xcode versions means

另一个堆栈溢出问题中,我看到旧 Xcode 版本中的此错误意味着

cannot invoke initializer for type '%type' with an argument list of type '...' expected an argument list of type '...'

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.

我在 swift4/xcode9 上。

Update

更新

After following the suggestion Use C.init(modelView: m) rather than C(modelView: m)the error changes in:

遵循建议后Use C.init(modelView: m) rather than C(modelView: m),错误更改为:

No 'C.Type.init' candidates produce the expected contextual result type '_?'

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?

比@vini-app 建议删除 UIViewController 以使其正常工作。我仍然不明白为什么当 UIViewController 存在时编译器不高兴。知道 C 有那个有效的 init 方法还不够吗?

回答by andyvn22

You just need to use initexplicitly whenever you're initializing a generic parameter rather than a "real" type:

您只需要init在初始化泛型参数而不是“真实”类型时显式使用:

self.c = C.init(modelView: m)

回答by rounak

Use C.init(modelView: m)rather than C(modelView: m). That should fix it.

使用C.init(modelView: m)而不是C(modelView: m). 那应该解决它。

回答by Vini App

Please check :

请检查 :

In your code you are doing like this C : MainViewControllerInterface, C : UIViewController.

在您的代码中,您正在这样做C : MainViewControllerInterface, C : UIViewController

It is treating Cas ViewController, then there is no initin ViewController like init(modelView: M)thats why its throwing error

它被C视为 ViewController,然后init在 ViewController 中没有像init(modelView: M)这就是它抛出错误的原因

public class Other<C, M> : NSObject where C : MainViewControllerInterface, M : MainControllerToModelInterface, C.MODELVIEW == M {
    var c : C?

    override init() {
        let m = M()
        self.c = C(modelView: m)
        super.init()
    }
}