1

I'm newbie for Swift UI. Which syntax parts do I have to study for understanding below code?

struct ContentView: View {
    var body: some View {
        VStack {
            Text("aaa")
            Text("bbb")
        }
    }
}

Below are what I want to understand

  1. is body read-only property? So is VStack { ... } implicitly returned value?
  2. VStack is struct, how brace can follow right after VStack? And how are Text Views able to be followed in brace? which syntax do I have to study to understand it?
buk
  • 59
  • 1
  • 4
  • 1) Yes 2) It's called a "trailing closure" – jnpdx Aug 05 '22 at 17:05
  • `VStack` is a structure and its initializer has a content parameter which expects a closure `content: () -> Content)`. As already mentioned in Swift when the last parameter is a closure you can use a "trailing closure" syntax and pass it after the initializer parentheses and if you are not passing any value inside you can omit them. `init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, content: () -> Content)` – Leo Dabus Aug 05 '22 at 17:10
  • `body` is an instance property with a getter and no setter. It is supposed to return `some View` a generic structure which its generic conforms to View protocol – Leo Dabus Aug 05 '22 at 17:15
  • View.body uses `@ViewBuilder` which is a type of ResultBuilder, refer https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID630. – user1046037 Aug 05 '22 at 17:39

0 Answers0