TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.