Posts

Showing posts with the label generics

Treehouse Intermediate Course - Part 12 (Closures 3)

Image
OK, so a change of plans of where I was yesterday. No matter - an entry before bed instead! Just enough to keep things ticking over. I'm then in Liverpool from Wednesday until Friday. Depending on my use of wifi/hotspot on the journey, I may be able to add in something during the journey there and/or back. Right, enough prelude and let's get going. A quick session! Start Time - 21:33 Map Standard Library Functions - certain aspects on Swift are already built in. So without the use of a closure - how to transform an array of ints so that they are doubled: let values = [ 1 , 2 , 3 , 4 , 5 ] var newArray = Array < Int >() for number in values {     newArray . append (number * 2 ) } This is the alternative.  let doubledArray = values . map { $0 * 2 }  So interesting tech speak here. The first is the imperative approach - showing line by line instruction. The declarative - the closure one - shows the end result wi...

Treehouse Intermediate Course - Part 9 (Generics - Consolidation!)

So two things... I am ending up doing another entry today and I will be consolidating generics after all! I have a little bit of time so just a good half hour or so on summarising generics then doing a little extra practice, based on what I can find! Start Time - 20:06 Reviewing Treehouse Course Wow there was a lot to this course! By 'review' I mean to go through again, rather than critique. First off, I'm just going to skim through each of my previous six entries and do a summary based on those. Writing Repetitive Code When I started the generics course, I thought this would be the use of any - down casting and upcasting as appropriate. Not really at all! Example of swapping values. Had to use inout keyword in the parameter/argument labels part as that is the only way of making them variables! The point with this whole bit is that it is limited with one type, or you make a function for each type - intSwap, stringSwap etc. Use of generics comes in here to use th...

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

Treehouse Intermediate Course - Part 6 (Generics 4)

Image
So it's been a few days! But I'm back for more generics! Before I begin, I'm just going to recap what I've been looking at most recently.  Start Time - 20:32 OK so before it was the general info about generics, including the use of protocols and multiple parameters. Most recently, it was about generic types in the standard library. This was basically about what is in the swift documentation and how actual quite a few of the types are generics! So a dictionary shows generics in the sense that, once defined, it has specific types that must be conformed too. Going to skim over a bit more...  Yes, some was on other ways to create arrays, optionals and also what's 'under the hood' for various bits. Generic types are used all the time including for custom data structures. OK, let's move on! Linked Lists Array - ordered set of data etc. etc. A linked list - a set of data items that are connected to one another. Each item is a NODE. Two types - sin...