How can some structs in SwiftUI library have read only properties declared like in protocol, without defining them

For example, in the following code, body property is declared like in protocol, declared but not defined.

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public struct List<SelectionValue, Content> : View where SelectionValue : Hashable, Content : View {

    /// Creates a list with the given content that supports selecting multiple
    /// rows.
    ///
    /// On iOS and tvOS, you must explicitly put the list into edit mode for
    /// the selection to apply. To do that, either add an ``EditButton`` to
    /// your user interface, or modify the ``EnvironmentValues/editMode``
    /// value directly.
    ///
    /// - Parameters:
    ///   - selection: A binding to a set that identifies selected rows.
    ///   - content: The content of the list.
    @available(watchOS, unavailable)
    public init(selection: Binding<Set<SelectionValue>>?, @ViewBuilder content: () -> Content)

    /// Creates a list with the given content that supports selecting a single
    /// row.
    ///
    /// On iOS and tvOS, you must explicitly put the list into edit mode for
    /// the selection to apply. To do that, either add an ``EditButton`` to
    /// your user interface, or modify the ``EnvironmentValues/editMode``
    /// value directly.
    ///
    /// - Parameters:
    ///   - selection: A binding to a selected row.
    ///   - content: The content of the list.
    @available(watchOS, unavailable)
    public init(selection: Binding<SelectionValue?>?, @ViewBuilder content: () -> Content)

    /// The content of the list.
    public var body: some View { get }

    /// The type of view representing the body of this view.
    ///
    /// When you create a custom view, Swift infers this type from your
    /// implementation of the required ``View/body-swift.property`` property.
    public typealias Body = some View
}

When I do this, the code won't compile, for example:

struct TestStruct {
	var body: String {get}
}

Thanks.

Oh, I probably got it. Maybe the SwitfUI code XCode shows is just 'header files' and not complete. Is this right?

4 Likes

Yep, exactly.

1 Like

Thank you. :)

The official term for this is “interface file”. Xcode can generate interface files for your own code if you follow these steps. :)

2 Likes

Thank you. This is really helpful :heart:

1 Like