SwiftUI 你必须知道的10个子视图的限制

先来看看下面的代码

一、正常的代码,能够运行swift

struct ContentView: View {
    
    var body: some View {
        
            Group {
                Text("1")
                Text("2 ")
                Text("3 ")
                Text("4 ")
                Text("5 ")
                Text("6 ")
                Text("7 ")
                Text("8 ")
                Text("9 ")
                Text("10 ")
                //Text("11 ")
            }
    
    }
}

二、会提示编译错误的代码xcode

struct ContentView: View {
    
    var body: some View {
        
            Group {
                Text("1")
                Text("2 ")
                Text("3 ")
                Text("4 ")
                Text("5 ")
                Text("6 ")
                Text("7 ")
                Text("8 ")
                Text("9 ")
                Text("10 ")
                Text("11 ")
            }
    
    }
}

build时会出现下面的错误app

Argument passed to call that takes no arguments

真正的缘由

xcode的提示实在是太莫名其妙了,我经过查看文档才发现。SwiftUI 的container是有数量限制,具体限制多少呢。文档里面没有写明(也多是咱们没有找到),可是文档下面的buildblock没有超过10的ui

有图有真相

后来我又google一下,果真有这个限制。google

受影响的其余容器

下面的容器都是有10个对象的限制spa

  • VStack
  • HStack
  • ZStack
  • Group
  • List
  • 将来的容器

为何有这个限制

我认为应该是apple不但愿咱们建立过于复杂的单页视图,但愿咱们尽快进行模块划分。code

参考资料

更多SwiftUI教程和代码关注专栏

相关文章
相关标签/搜索