|
I'm trying to understand a little how the View protocol from SwiftUI works. So we have this:
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. |
|
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. |
|
I think Self always refer to the conforming Type of the protocol, for example:
Here, But I dont understand why they refer to
|
|
If you have a type somewhere else in your code called By using
Yes, and those types can get very complicated very fast. This
has a
* example from Thinking in SwiftUI v2.0 by Chris Eidhof and Florian Kugler |
|
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. |
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.
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.
Link copied to your pasteboard.