Swift泛型:非名义类型不支持显式初始化 [英] Swift generics: Non-nominal type does not support explicit initialization
问题描述
所以我想了解泛型协议和类:
protocol ListPresenterType where View.PDO.SW == Dispatcher .SW {
associatedtype Dispatcher:ListDispatcherType
associatedtype View:ListViewType
init(调度程序:调度程序,状态:@escaping(_ state:AppState) - >(ListState< Dispatcher .SW>))
func attachView(_ view:View)
...
}
我从另一个泛型类开始:
pre $ class $ AbstractListViewController<演示者:ListPresenterType,PDO:ListPDOCommonType,...>:ListViewType,...其中PDO.SW == Presenter.Dispatcher.SW,... {
func configure(withBla:bla){
...
presenter = Presenter(dispatcher:dispatcher,state:state)
}
func someFunc(){
presenter.attachView(self)//错误:无法用类型的参数列表调用'attachView'。 ..
}
据我所知,我试图初始化符合一个通用的协议,它工作得很好,但视图类型必须与我试图在 attachView(:)
中提供它的内容不一致。
然后我试着用具体的视图初始化它,改变 init
:
<$ (Dispatcher:Dispatcher,view:View,state:@escaping(_ state:AppState) - >(ListState< Dispatcher.SW>)){
self.view = view
...
}
并且在 AbstractListViewController
:
presenter = Presenter(dispatcher:dispatcher,view:self,state:state)
获得这个臭名昭着的错误:
非名义类型'Presenter'不支持显式初始化
这里有相关游乐场的要点:
- 初始化失败,出现上述错误: https://gist.github.com/nikans/53c3ea146ceb12dc8461f7ba8a81793d
attachView(:)
) https://gist.github.com/nikans/0fde838846ffa9ff2da48c923f850625
请注意,每个空白协议实际上都是一个通用协议,我刚刚删除了不必要的细节。
我想了解:
- 是什么让突然出现的名义类型非名义令人惊讶的是,它是一个符合通用协议的泛型参数,但我不明白这个因果关系。)
- 我听说过类型擦除的一些事情,但是如果它是适用于此。
谢谢。
)错误,如非名义类型'%type'不支持显式初始化
简单地等于不匹配类型
错误少于错误的Xcode(如果这是一件事情):不能用'...'类型的参数列表调用类型为'%'的初始值设定项,期望类型为'...'的参数列表
So I'm trying to understand generic protocols and classes:
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)
...
}
And I'm initiating it from another generic class:
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 ...
}
As I understand, I'm trying to initialize a type conforming to a generic protocol, which works just fine, but the type of View must be inconsistent with what I'm trying to feed it in attachView(:)
.
Then I try to initialize it with concrete view, changing init
:
init(dispatcher: Dispatcher, view: View, state: @escaping (_ state: AppState)->(ListState<Dispatcher.SW>)) {
self.view = view
...
}
And in AbstractListViewController
:
presenter = Presenter(dispatcher: dispatcher, view: self, state: state)
And getting this infamous error:
Non-nominal type 'Presenter' does not support explicit initialization
Here're gists with relevant playgrounds:
- Successful init (though cannot invoke
attachView(:)
) https://gist.github.com/nikans/0fde838846ffa9ff2da48c923f850625 - Init failing with aforementioned error: https://gist.github.com/nikans/53c3ea146ceb12dc8461f7ba8a81793d
Please notice that every empty protocol there is in fact a generic protocol, I've just removed the unnecessary details.
I'd like to understand:
- What makes a "nominal" type "non-nominal" all of a sudden (nothing to be surprised of, it's a generic parameter conforming to a generic protocol, but I don't understand the causation).
- I heard something about type erasure, but didn't quite get if it is applicable here.
Thanks.
So it appears that in Xcode9 (beta 6) errors like Non-nominal type '%type' does not support explicit initialization
simply equals the mismatching types
error in less buggy Xcode (if that's a thing): cannot invoke initializer for type '%type' with an argument list of type '...' expected an argument list of type '...'
这篇关于Swift泛型:非名义类型不支持显式初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!