[英]How to add a SwiftUI some View Type as a Requirement to an Protocol?
语境
我有一个Protocol
,并希望它有一个SwiftUI
some View
Type
作为其Requirements
之一。 我知道,将Model
与View
混合可能不是最优雅的解决方案,但是,每次我想使用View
时切换所有不同的符合Types
也不是很漂亮。 尽管如此,我在实现时遇到了问题并得到了以下Compiler Error
:
'some' 类型不能是协议要求的返回类型; 您是要添加关联类型吗?
代码
protocol Component {
var row: some View { get } // -> Compiler Error thrown in this Line.
}
// There are actually many more Types conforming to Component.
enum ComponentA: Component {
var row: some View {
Text("Component A")
}
}
struct ComponentsView: View {
var body: some View {
ForEach(components) { component in
component.row
}
}
}
问题
如何实现我的目标,即只需定义一次选择合适的 SwiftUI 视图?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.