Swift泛型:非标称类型不支持显式初始化

时间:2017-09-07 01:03:40

标签: swift generics xcode9beta6

所以我试图理解通用协议和类:

protocol ListPresenterType where View.PDO.SW == Dispatcher.SW {
    associatedtype Dispatcher: ListDispatcherType
    associatedtype View: ListViewType

    init(dispatcher: Dispatcher, state: @escaping (_ state: AppState)->(ListState<Dispatcher.SW>))

    func attachView(_ view: View)
    ...
}

我是从另一个通用类开始的:

class AbstractListViewController<Presenter: ListPresenterType, PDO: ListPDOCommonType, ...>: ListViewType, ... where PDO.SW == Presenter.Dispatcher.SW, ... {
    func configure(withBla: bla) {
        ...
        presenter = Presenter(dispatcher: dispatcher, state: state)
    }

    func someFunc() {
        presenter.attachView(self) // ERROR: Cannot invoke 'attachView' with an argument list of type ...
}

据我所知,我正在尝试初始化符合通用协议的类型,该协议工作得很好,但View的类型必须与我尝试在{{{{{{{{{{{{{{ 1}}。

然后我尝试用具体视图初始化它,改变attachView(:)

init

init(dispatcher: Dispatcher, view: View, state: @escaping (_ state: AppState)->(ListState<Dispatcher.SW>)) { self.view = view ... }

AbstractListViewController

得到这个臭名昭着的错误:

presenter = Presenter(dispatcher: dispatcher, view: self, state: state)

这里有相关游乐场的要点:

  1. 成功初始化(虽然无法调用Non-nominal type 'Presenter' does not support explicit initialization https://gist.github.com/nikans/0fde838846ffa9ff2da48c923f850625
  2. Init因上述错误而失败:https://gist.github.com/nikans/53c3ea146ceb12dc8461f7ba8a81793d
  3. 请注意,每个空协议实际上都是通用协议,我刚刚删除了不必要的细节。

    我想明白:

    1. 是什么让#34;名义上的&#34;类型&#34;非名义&#34;突然之间(没有什么可惊讶的,它是符合通用协议的通用参数,但我不理解因果关系)。
    2. 我听说过有关类型擦除的问题,但是如果它适用于此处则没有。
    3. 感谢。

1 个答案:

答案 0 :(得分:3)

因此,在Xcode9(测试版6)中,似乎Non-nominal type '%type' does not support explicit initialization之类的错误只是等于错误较少的Xcode中的mismatching types错误(如果这样做的话):cannot invoke initializer for type '%type' with an argument list of type '...' expected an argument list of type '...'