Art*_*ich 5 swift swift-protocols
我有以下协议:
protocol ViewCreator {
associatedtype ResultView: View
@ViewBuilder func createView() -> ResultView
}
Run Code Online (Sandbox Code Playgroud)
我想获取它的类型,例如获取它的名称。所以我写了下面的代码:
let typeName = String(describing: ViewCreator.self)
Run Code Online (Sandbox Code Playgroud)
但出现以下错误:
协议“ViewCreator”只能用作通用约束,因为它具有 Self 或关联的类型要求。
在这种情况下,我如何获得具有关联类型的协议的“自身”?
PS我的范围内没有该协议的实现。
小智 4
这与不使用关联类型时没有什么不同。
(any ViewCreator).self
Run Code Online (Sandbox Code Playgroud)
String(describing: (any ViewCreator).self) // "ViewCreator"
Run Code Online (Sandbox Code Playgroud)
将来,您可以选择使用主要关联类型来提供限制。但就目前而言,这将编译并执行,但要考虑<<< invalid type >>>。
protocol ViewCreator<ResultView> {
Run Code Online (Sandbox Code Playgroud)
(any ViewCreator<EmptyView>).self
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
472 次 |
| 最近记录: |