GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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?

2      

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>)>>

4      

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

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

3      

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.

2      

Watch Paul video on How to use opaque return types.

2      

@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?

2      

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.

2      

@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.

2      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

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.