Generics in Swift: Protocols with associated types

Опубликовано: 05 Ноябрь 2024
на канале: Swift and Tips
9,810
353

Hello guys, welcome to another video! Today we will continue with the Generics series talking about protocols with associated types. SwiftUI views were made with these special protocols, How can we create our own associated types? Can I create a generic protocol with square brackets?
How does Swift infer associated types?... we will solve all those questions and also see how to use protocols when we found the error "Protocol can only be used as a generic constraint...".

Before continue I recommend you to watch the first episode:
Part 1:    • Swift Generics: The Key to Reusable a...  

Content:
0:00 - Intro
0:29 - Associated types in SwiftUI
0:52 - What are associated types?
6:43 - Generic type with a protocol with associated type
9:52 - Extending from a protocol with associated type
12:28 - How to solve "Protocol can only be used as a generic constraint ..."?
14:50 - Outro

------
References:
https://docs.swift.org/swift-book/Lan...
https://developer.apple.com/videos/pl...
https://www.swiftbysundell.com/basics...

Source code showed in this video: https://github.com/pitt500/swift-and-...

------
Other videos in the channel:
Protocols in Swift:    • Protocols  
UIKit in SwiftUI:    • UIKit in SwiftUI  
Struct vs Classes:    • Structs vs classes in  Swift  

-----
Don't forget to subscribe to the channel, like it, and follow me on social media:

Twitter:
  / swiftandtips  
  / pitt500  

Finally, ask me for a video you would like to see next 😄!

Credits:
Music by Wataboi from https://pixabay.com/
Video edited with LumaFusion