BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Help me understand the View protocol

Forums > SwiftUI

I'm trying to understand a little how the View protocol from SwiftUI works. So we have this:

protocol View {
associatedType Body: View

var body: Self.Body {get}
}

I have two questions about this:

Is Body placeholder replaced with the actual View type (for example Text, Button, Image, VStack, etc?)

What does "Self.Body" means?

Give me some examples please.

2      

Maybe this will help - Self vs self

So if you pass 'Text' to the 'View', 'Self.Body' will be essentially the same as 'Text.Body', and you say you can pass anything that conforms to the 'View' protocol.

2      

I think Self always refer to the conforming Type of the protocol, for example:

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

Here, Self.Body would be MyCustomView.Body

But I dont understand why they refer to var: Self.Body instead of just var: Body

2      

But I dont understand why they refer to var: Self.Body instead of just var: Body

If you have a type somewhere else in your code called Body, how is the compiler to know which Body you are referring to?

By using Self.Body, the compiler knows you are referring to the associatedType Body of the type that conforms to the View protocol.

Is Body placeholder replaced with the actual View type (for example Text, Button, Image, VStack, etc?)

Yes, and those types can get very complicated very fast.

This View:

struct ContentView: View {
    @State var counter = 0 

    var body: some View {
        VStack {
            Button(action: { counter += 1 }, label: {
                Text("Tap me!")
                    .padding() 
                    .background(Color(.tertiarySystemFill))
                    .cornerRadius(5)
            })
            if counter > 0 {
                Text("You've tapped \(counter) times")
            } else {
                Text("You've not yet tapped")
            }
        } 
    }
}

has a body whose type is this:

VStack<TupleView<(Button<ModifiedContent<ModifiedContent<ModifiedContent<Text,_PaddingLayout>,_BackgroundModifier<Color>>,_ClipEffect<RoundedRectangle>>>,_ConditionalContent<Text, Text>)>>

* example from Thinking in SwiftUI v2.0 by Chris Eidhof and Florian Kugler

4      

Thank you very much, now is clear for me, I didn't know Swift would confuse if I have something else called Body. Now it makes sense. SwiftUI is making sure that Body is coming from "MyCustomView.Body" and not some independent Body type.

2      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.