如何解决Swift泛型类型转换问题
我有这样的代码如何解决Swift泛型类型转换问题,swift,generics,swiftui,viewmodifier,Swift,Generics,Swiftui,Viewmodifier,我有这样的代码 import SwiftUI struct TabItemModifier<TabItem>: ViewModifier where TabItem: View { var tabItem: () -> TabItem func body(content: Content) -> some View { return TabItemView(tabItem: tabItem, content: content)
import SwiftUI
struct TabItemModifier<TabItem>: ViewModifier where TabItem: View {
var tabItem: () -> TabItem
func body(content: Content) -> some View {
return TabItemView(tabItem: tabItem, content: content)
}
}
struct TabItemView<TabItem, Content> : View where Content: View, TabItem : View {
var tabItem: () -> TabItem
var content: Content
var body: some View {
content
}
}
extension View {
func withTabItem<V>(@ViewBuilder _ label: @escaping () -> V) -> some View where V: View{
ModifiedContent(content: self, modifier: TabItemModifier(tabItem: label))
}
}
func menuTabItem<T>(tag: Int, @ViewBuilder _ tabItem: @escaping () -> T) -> some View
where T: View {
ModifiedContent(content: AnyView(self),
modifier: Click5MenuItemModifier(
tag: tag,
menuItem: nil,
tabItem: AnyView(tabItem())
)
)
}
struct MenuItemModifier: ViewModifier {
var tag: Int
var menuItem: AnyView?
var tabItem: AnyView?
func body(content: Content) -> some View {
return content
}
}
但这种铸造是不可能的
更新
我这样改变了
var body: some View {
MenuView {
Page1()
Page2()
.menuTabItem(tag: 1) {
TabItemView(systemImage: "person", title: "Tab 2")
}
这个ViewModifier的新实现非常简单,如下所示
import SwiftUI
struct TabItemModifier<TabItem>: ViewModifier where TabItem: View {
var tabItem: () -> TabItem
func body(content: Content) -> some View {
return TabItemView(tabItem: tabItem, content: content)
}
}
struct TabItemView<TabItem, Content> : View where Content: View, TabItem : View {
var tabItem: () -> TabItem
var content: Content
var body: some View {
content
}
}
extension View {
func withTabItem<V>(@ViewBuilder _ label: @escaping () -> V) -> some View where V: View{
ModifiedContent(content: self, modifier: TabItemModifier(tabItem: label))
}
}
func menuTabItem<T>(tag: Int, @ViewBuilder _ tabItem: @escaping () -> T) -> some View
where T: View {
ModifiedContent(content: AnyView(self),
modifier: Click5MenuItemModifier(
tag: tag,
menuItem: nil,
tabItem: AnyView(tabItem())
)
)
}
struct MenuItemModifier: ViewModifier {
var tag: Int
var menuItem: AnyView?
var tabItem: AnyView?
func body(content: Content) -> some View {
return content
}
}
func菜单项(标记:Int,@ViewBuilder\tabItem:@escaping()->T)->一些视图
其中T:视图{
修改内容(内容:AnyView(self),
修改器:单击5菜单项修改器(
标签:标签,
menuItem:无,
tabItem:AnyView(tabItem())
)
)
}
结构菜单修改器:视图修改器{
变量标记:Int
var menuItem:任何视图?
var tabItem:AnyView?
func正文(内容:内容)->某些视图{
返回内容
}
}
但问题是,我只能在MenuView中直接使用它。在Page1()中使用此修饰符会导致视图隐藏在Page1下,然后在MenuView的实现中我无法使用上述强制转换访问此修饰符,也没有对此修饰符链的引用。
TabItemView
是一个具有两个关联类型的结构。使用时必须指定具体类型。类似于…as?选项卡项目视图
。但是,无法指定不透明类型(视图
)。因此,我的建议是:
将
TabItemView
的content
更改为AnyView
,并保持tabItem
的类型与tabItems
的类型相同。tabItems.append(tabbed.tabItem)
应该可以工作。它不像我在TabItemView上做的那样工作,我只能处理类似于此ModifiedContent的内容,但我想在此处使用一些视图,而不是文本,可能是一些等价的视图,只需访问tabItem作为一些视图而不是特定的视图,并且视图也是一些通用视图而不是特定的文本请编辑问题以声明tabItems
,给出一个可以在我们结束时进行全面测试的示例,以了解问题。以这种方式修改视图层次结构看起来非常错误。SwiftUI视图是值类型,并且总是从头开始重新生成。它们描述了视图层次结构;它们不是实际的屏幕视图。你没有触及和修改现有的数据;您计算所需的所有选项卡,TabItemView要么将它们作为参数,要么从环境中读取它们。即使你能做到这一点,我希望它不会正确更新。谢谢@RobNapier,我想我需要阅读这篇关于“首选项”的文章,这将是解决这个tabItem视图添加问题的方法。因为我当前的解决方案效果很差。因为tabItems
是[AnyView],所以tabItem
也应该是AnyView
。