๐ฆViewBuilder
From Xcode 12, both switch and if let are supported in the ViewBuilder!
You typically use ViewBuilder
as a parameter attribute for child view-producing closure parameters, allowing those closures to provide multiple child views. ๐ SwiftUI โฉ View โฉ ViewBuilder
As for ViewBuilders, it is mainly used to create custom container views, which can also become a reusable view component. ๐ Understanding SwiftUI's ViewModifiers and ViewBuilders
A good rule of thumb for me has to be to use a modifier first, and only use a builder when the code patterns really pulled strongly for that syntax. ๐ View Builders - NetSplit.com
@ViewBuilder is one of the possible function builders. ๐ Swift with Majid
ViewBuilder as a Parameter
You typically use ViewBuilder as a parameter attribute for child view-producing closure parameters, allowing those closures to provide multiple child views.
้ๅธธ @ViewBuilder closure ๅ ทๆ @escaping ็็นๆง๏ผๅ ็บไธ่ฌ้ๅ closure ๆๅจ .init(content:) ไธญๅณ้ฒไพ๏ผ็ถๅพๅจ var body ไธญ็จๅฐ๏ผๅฆๆๆฒๆไบๅ ่จญ็บ @escaping ็่ฉฑ๏ผ้ฃ้บผ้ๅ closure ๅจ .init ไธญๅณ้ฒไพๅพ๏ผๆ้จ่ .init ็ตๆ่ๆถๅคฑ๏ผๅ ๆญคไนๆฒ่พฆๆณไบๅพๅจ var body ไธญ็จๅฐ๏ผๆไปฅๆ็ข็ compiler ้ฏ่ชคใ ๅ๏ผ้ฃ้บผ @escaping closure ๆๅญๆพๅจ้ฃ่ฃๅข ๐คโ ็ญ๏ผ้ๅธธๆๅญๆพๅจๅฝๆธๅค้จ็่ฎๆธๆ้ฃๅไธญใ
ไฝๅ้ไพ่ชช๏ผๅฆๆๅณ้ฒไพ็ @ViewBuilder closure ้ฆฌไธๅจ .init ไธญๅฐฑ็จๆไบ๏ผ้ๆๅฐฑไธ้่ฆๅ @escaping ้ๅๅฑฌๆงไบใ
Using ViewBuilder โญ๏ธ
ๅ ฑๆๅๅๆญฅ้ฉ๏ผ
Examples
Last updated