[英]How to return a View type from a function in Swift UI
I am trying to create a somewhat elegant navigation system for my App.我正在尝试为我的应用程序创建一个有点优雅的导航系统。 Below is a function that attempts to return a View type.
下面是一个试图返回 View 类型的 function。 This does not compile with:
这不编译:
func getView(view: String) -> View {
switch view {
case "CreateUser":
return CreateNewsView()
default:
return nil
}
}
The above results in a compile error: Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
以上导致编译错误:
Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
Thank you for your help.谢谢您的帮助。
As of Swift 5.3 @hồng-phúc Answer is somehow right, just needs adding the @ViewBuilder Property explicitly.截至 Swift 5.3 @hồng-phúc 答案在某种程度上是正确的,只需要明确添加 @ViewBuilder 属性。
@ViewBuilder func getView(view: String) -> some View {
switch view {
case "CreateUser":
Text(view)
case "Abc":
Image("Abc")
default:
EmptyView()
}
}
Side note: Please avoid using String Literals.旁注:请避免使用字符串文字。 Better use an enum.
最好使用枚举。
I managed to fix this by using the AnyView() wrapper:我设法通过使用 AnyView() 包装器来解决这个问题:
func getView(view: String?) -> AnyView {
switch view {
case "CreateUser":
return AnyView(CreateNewsView())
default:
return AnyView(EmptyView())
}
}
Making AnyView() wrapper制作 AnyView() 包装器
func getView(view: String?) -> AnyView {
if view == "CreateUser" {
return AnyView(CreateNewsView())
}
return AnyView(EmptyView())
}
I use the extension .eraseToAnyView ()
to make func with some View easier:我使用扩展名
.eraseToAnyView ()
使某些 View 的功能更容易:
extension View {
public function eraseToAnyView () -> AnyView {
AnyView (on its own)
}
}
The final solution will look like this:最终解决方案将如下所示:
func getView (view: String?) -> AnyView {
if view == "CreateUser" {
return CreateNewsView().eraseToAnyView()
}
return EmptyView().eraseToAnyView()
}
I think this is the most concise solution.我认为这是最简洁的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.