BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: I still don't understand "some View"

Forums > SwiftUI

Paul says:" View by itself doesn't mean anything. SwiftUI needs to know what kind of view it is returning."

So, how does the word some solve that problem? If all components like Text or Image conform to protocol View, then how does the word some specifiy which type it is?

   

It doesn't. The some keyword tells us that we are dealing with something that is a View but we dont't need to know exactly what kind of View it is. The compiler knows, but we don't need to.

With Text or Image it kinda doesn't make much difference, but SwiftUI types can get far, far more complex and strange and using some View saves us from having to write things like

VStack<TupleView<(Button<ModifiedContent<ModifiedContent<ModifiedContent<Text, _PaddingLayout>,_BackgroundModifier<Color>>, _ClipEffect<RoundedRectangle>>>, _ConditionalContent<Text, Text>)>>

2      

This is explained very well in this chapter on Opaque Types:

https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html

1      

Abdo asks:

If Text or Image conform to protocol View, then how does the word some specifiy which type it is?

Consider an adventure game where you have a door that only Wizards can walk through.

But in this game, players have modified their Wizards.

let gandalf = Wizard( name: "Gandalf")
                         .withStaff("Golden Eye") // lots of modifiers
                         .experience(100)
                         .robe(color: Color.red)
                         .wealth(gold: 10_000)

let harry = Wizard( name: "Harry Potter")  // lots of modifiers
                         .experience(50)
                         .house(name: "Gryffindor")
                         .pet(type: Pets.owl, name: "Hedwig")

You and I can recognize these are both wizards. However the Swift compiler sees these as complex wizard types.

gandalf is a wizard.withStaff.andExperience.andRobe.andWealth
harry is a wizard.withExperience.andHouse.andPet

The compiler sees these as two different Types.

In the game, you might have hundreds of wizards with many, many variations. In game logic, this would be a nightmare to keep track of. Instead, you inform the door that after all the modifications have been applied, you only care that only some Wizard can pass through the door.

   

Watch Paul video on How to use opaque return types.

   

@Obelix
So after adding modifications the View comonent can get very complex and maybe conform to other protocols and the word some tells the compiler to only focus on the View part?

   

Abdo asks:

So after adding modifications the View component can get very complex and maybe conform to other protocols and the word some tells the compiler to only focus on the View part?

Aye!

That's what @rooster said! And @twoStraws in the video noted by @Nigel.

@rooster shows an actual example of a complex view. It hurts my eyes just trying to interpret it. Can you understand what Type this view is?

It's a button, with some padding, and a background color. Wait. It also has a rounded rectangle clip shape, with some Text. It looks like the whole thing is in a VStack? No wait, is the Text in the button? or in the VStack??

This shows that we are not equipped to interpret the exact type of a View after applying a number of modifiers. It's just insane. Yet, the compiler is GREAT at doing this.

Instead of trying to code and understand the final Type, we just tell the compiler to look for some View.

VStack<TupleView<(Button<ModifiedContent<ModifiedContent<ModifiedContent<Text, _PaddingLayout>,_BackgroundModifier<Color>>, _ClipEffect<RoundedRectangle>>>, _ConditionalContent<Text, Text>)>>

I hope I said the same thing by using an abstract (wizard and door) example.

   

@Obelix

I hope I said the same thing by using an abstract (wizard and door) example.

Actually, I find it much easier to understand a difficult concept if the other person used metaphors and examples. I wish Paul would do that more.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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.