0

我是 iOS Development 的新手。在这里,我尝试使用 foreach 循环获取不同的目标视图

struct NewView : View
{
var arrayofviews //here I need array of views
var body:some View
{
  List
  {
   ForEach(array)
   {(item) in
      NavigationLink(destination: item)
      {
       Text("Click Here")
      }
   }
  }
}
}

我试过以下

var arrayofviews=[view1(),view2(),view3()] as! [Any]

然后我收到此错误无法将“view1”类型的值转换为预期的元素类型“Array.ArrayLiteralElement”(又名“AnyView”)

这里 view1,view2,view3 是自定义视图

struct view1:View
{
var body:Some View
{
Text("...")
...
}
}
//similarly view2 and view3 also
4

1 回答 1

1
struct NewView : View
{
var arrayofviews=[AnyView(view1()),AnyView(view2()),AnyView(view3())] as! [AnyView] //This helped
var body:some View
{
  List
  {
   ForEach(0..<array.count)
   {(index) in
      NavigationLink(destination: array[index])
      {
       Text("Click Here")
      }
   }
  }
}
}

感谢“Mohammad Rahchamani”的建议!!!

于 2020-09-30T11:31:58.253 回答