好奇body奇怪的初始化语法:Some View in ContentView in SwiftUI

Curious about the weird initialization syntax of body: Some View in ContentView in SwiftUI

默认代码:

struct ContentView: View {
    var body: some View {
        Text("Hello World!")
    }
    
}

我猜上面的代码应该等价于下面的代码:

struct ContentView: View {
    var body: some View = {
        Text("Hello World!")
    }()
    
}

或者更全面:

struct ContentView: View {
    var body: some View = {
        () -> Text in
            return Text("Hello World!")
    }()
    
}

我只想知道在哪里可以找到第一个代码块中 body 的此初始化语法的引用?我在 swift.orgthe swift programming lanauage 一书的闭包章节中没有找到任何关于此语法的描述。

您错误地将 属性 body 识别为已存储的 属性。它实际上是一个computed property。将其更改为存储的 属性 会显着改变代码的语义。

相当于:

var body: some View {
    get {
        return Text("Hello World!")
    }
}

returnget可以省略Shorthand Property Declaration and a read-only computed property

If the entire body of a getter is a single expression, the getter implicitly returns that expression.

You can simplify the declaration of a read-only computed property by removing the get keyword and its braces