UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 60: Key Points

Forums > 100 Days of SwiftUI

I am so confused ... all of this discussion about type erasures in the Key Points section ... there was no mention of this in any of the three projects it references. We didn't cover AnyView or @ViewBuilder in any of the projects leading up to this point.

I feel like I'm crazy ... did I miss a lesson somewhere??

2      

@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

3      

Thank you @obelix for the thoughtful reply!

I must have misread. It seemed like Paul was implying this was something we had previously covered. I've taken time to absurb these Key Points and appreciate your breakdown as well. Very helpful, cheers!

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

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.