LibraryContentBuilder
A function builder for generating arrays of library items without requiring full array literal syntax.
Declaration
@resultBuilder struct LibraryContentBuilderOverview
Use the library content function builder to simplify the implementation of protocol requirements from you which provide arrays of library items. For example, without the builder, you would have to explicitly put items in an array in a views implementation:
struct LibraryViewContent: LibraryContentProvider {
var views: [LibraryItem] {
[
LibraryItem(MyFirstView()),
LibraryItem(MySecondView())
]
}
}With the builder, you can omit the array literal syntax:
struct LibraryViewContent: LibraryContentProvider {
@LibraryContentBuilder
var views: [LibraryItem] {
LibraryItem(MyFirstView())
LibraryItem(MySecondView())
}
}In practice, the Swift compiler infers the need for a library content builder attribute and adds it at build time, so that you never need to explicitly write the attribute in your code, even though it’s technically in use:
struct LibraryViewContent: LibraryContentProvider {
var views: [LibraryItems] {
LibraryItem(MyFirstView())
LibraryItem(MySecondView())
}
}