I’m reading SwiftUI materials and it’s said that view modifiers for example:
struct ByeView: View {
var body: some View {
Text("Bye bye, world!")
.font(.headline)
}
}
creates a new view with .headline
font and returns the view.
So I wonder if it’s more like:
func font(_ font: UIFont) -> Text {
Text(text, font: font)
}
rather than:
func font(_ font: UIFont) -> Text {
self.font = font
return self
}
I feel for event modifiers, since they may not have to modify the view, there’s no need to "create a new view with modified aspects", but not sure about the modifiers that do adjust the views.
Thanks!
2
Answers
TLDR
We can’t know for sure as the implementation detail is hidden.
My point of view
I might be wrong but I think it’s possible that neither of modifying an instance of a View or assigning to a property that you have described are actually happening, but we can’t know for sure since the implementation is private.
Since SwiftUI is a declarative framework, where you describe what you want to get and the OS takes care of that, it could be even that by writing:
Internally it could become just about any representation such as JSON that the parser could read.
Essentially no instance of any object (apart from the description) representing a View, Color or CornerRadius could exist before its initialization based on the description and current state, thus there would be no instance holding properties (such as font) that could be assigned or altered before the final View is initialized.
First, note that an implementation such as
does not compile.
font
would need to bemutating
for this to work, but it isn’tmutating
. That said, this would have compiled:But this has another problem. This means that
font
now has a side effect:So I think it is very likely that the actual implementation involves calling the initialiser, rather than
return self
. This also matches the wording in the documentation’s wording that a "new view" is "created".For modifiers that return
some View
, you can check the type they return usingtype(of:)
. You will most likely see that the type they return is different fromself
, which means they definitely do notreturn self
! After all, that’s one of the reasons why the opaque typesome View
is used in the signature – to hide the internal types Apple used to implement these modifiers.For me, this prints:
ModifiedContent<Text, _AppearanceActionModifier>
, notText
, so clearly a new view is created here. (See alsoModifiedContent
)