Posts

Showing posts with the label associated types

Treehouse Intermediate Course - Part 8 (Generics 6)

Image
So the sixth and final part on generics! It's been tough but I've accepted that it's all part of learning Swift at a deeper level, rather than learning specific code for particular projects! Let's do this! Start Time - 15:46 So creating a couple of protocols - linked to views and data. By specifying a protocol as the type, we can specify any type that conforms to that protocol. So errors are coming up. Here's Pasan's code - protocol DataProvider {          associatedtype Object          func object(atIndex index: Int ) -> Object } protocol ConfigurableView {          associatedtype Data          func configure(with data: Data ) } class ViewController<View: ConfigurableView , DataSource: DataProvider > {          let view: View     let data: DataSource          init ...

Treehouse Intermediate Course - Part 7 (Generics 5)

Image
The roll continues! So I will do similar to the other week - do several entries within one blog as it may be a bit stop-start. I've got around 20 minutes now just to make a start. By the end of today I will be finished with generics! I will then do a follow up consolidation entry with extra practice before moving on! Start Time - 12:53 Associated Types Can protocols be made generic? It is already in the sense that any type can conform to a protocol. But what about the internal? As in the code that goes behind the scenes... Stack - a bit like an array but more limited. The main point is the order - last in, first out. That makes sense. Associated type for the function - you can use a placeholder name for the type that is used in the protocol. The actual type is not specified until we actually adopt the protocol. Here is the protocol - protocol Stack {          associatedtype Element          mutating func push...