首页 > 解决方案 > 在 swiftUI 中创建表单究竟意味着什么?

问题描述

var body: some View {
    Form{
        Text("Hello, World")
    }
}

在这段代码中,这段代码是什么

 Form{ Text("Hello, World") }

方法?这是在创建“结构表单”的实例吗?或者它是创建一个“结构形式”的实例并只是添加一个闭包?还是我必须将其称为函数生成器?

标签: swiftxcodestructclosuresswiftui

解决方案


它正在调用Form构造函数

/// A container for grouping controls used for data entry, such as in settings
/// or inspectors.
///
/// - SeeAlso: `Section`, which can be used to add sections between groups of
///     content.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct Form<Content> : View where Content : View {

    public init(@ViewBuilder content: () -> Content)

所以

Form{ Text("Hello, World") }

相当于

Form.init(content: { () -> Text in
    return Text("Hello, World") 
})

推荐阅读