@mj is going crazy....
did I miss a lesson somewhere??
Hopefully you've picked up that Text("That's Crazy")
is a bona-fide view
?
Also Text("That's Crazy").font(.title)
is also a bona-fide view
?
What @twoStraws is sharing in Day 60: Key Points is that these two are not the same type of view. They are different types.
Consequently, you cannot have code like this:
var randomText: some View {
// It's possible to return ONE of TWO TYPES of view here.
if Bool.random() {
return Text("That's Crazy")
} else {
return Text("That's Crazy").font(.title) // <- This is a different view type.
}
}
var body: some View { // This requires a single type of view
randomText // <- But this can generate TWO different view types. WRONG!
}
So, his Key Point lesson continues by giving you options.
One option is to erase the type, so SwiftUI doesn't know you have two different possibilities.
Yes. You are tricking the compiler by erasing the types. @twoStraws uses Group
to trick the compiler.
Convince yourself that Group
is hiding the two types of Text()
views:
var randomText: some View {
Group { // <- Compiler thinks you're only creating one view type... a GROUP
// Dear compiler:
// Please don't look behind the curtain. Pay no attention to this....
if Bool.random() {
return Text("That's Crazy")
} else {
return Text("That's Crazy").font(.title) // <- This is a different view type.
}
}
}
var body: some View { // This requires a single type of view
randomText // <- Fooled the compiler! Only providing ONE type, a Group
}
You didn't miss a lesson. The lesson is IN the Day 60: Key Points
text.
See -> Day 60: Key Points