在使用SwiftUI時,我喜歡創建回傳視圖的函式,這只是一種分離和清理代碼的簡單方法。像這樣:
func deleteButton() -> some View { return (Button. ..) }
這樣做的效果很好,但是當我試圖回傳一個串列時,像這樣:
func itemsList() -> List<Never, some DynamicViewContent> { . ... }
我得到的錯誤是"'一些'型別只對屬性和下標的宣告型別以及函式的回傳型別實作"。 我試著不使用 "some",只使用 "DynamicViewContent "而不使用List,但都沒有成功。
有人能幫我解決這個問題嗎?
uj5u.com熱心網友回復:
選項#1:
回傳some View:
func myList() -> some View {
List(items, id:.self) { item in.
Text("Hello"/span>)
}
}
選項#2:
使用特定的型別。我能想到的最簡單的方法是將一個變數暫時設定為List。例如:
var myList = List( items, id:. self) { item in
Text("Hello"/span>)
}
如果我在myList上點擊選項,Xcode將顯示一個彈出視窗,顯示myList被推斷為的確切型別。你可以將其作為你的函式的回傳型別。在我設計的例子中,它是List<Never, ForEach<[GameModel], GameModel, Text>>/code>,其中items是[GameModel]
uj5u.com熱心網友回復:
你可以指定一個通用引數來代替:
func itemsList<Content: DynamicViewContent>() -> List<Never, Content> { }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/308568.html
標籤:
下一篇:<p>我正在使用FirebaseStorage進行認證/注冊,用戶可以在注冊時上傳圖片,但我不斷收到關于<strong>用戶沒有任何權限的錯誤。</strong>當
