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教程和代碼關注專欄

相關文章
相關標籤/搜索