Posts

Showing posts with the label trailing closures

Ray Wenderlich Functions and Types Course - Part 3

Image
Here we go! Getting back into a routine with this! Straight on to continue closures... Start Time - 19:45 Closure Syntax Writing closures inline is one of the main benefits. Using shorter syntax can make them easier but can also make it more complicated to figure out... This old chestnut I've seen before. let longClosure = { (a: Int , b: Int ) -> Int in     return a * b      } longClosure ( 4 , 3 ) let noParameterTypes = { (a, b) -> Int in     a * b      } let noReturnTypes : Operate = { (a, b) in     a * b } let shortClosure : Operate = { $0 * $1 } noParameterTypes ( 4 , 3 ) noReturnTypes ( 4 , 3 ) shortClosure ( 4 , 3 ) The difference is that there was a type alias with the closure signature there.  Here is a point about trailing closures -  // -------------------------------------- func printResult ( _ a: Int , _ b: Int , _...

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 11 (Closures 2)

Image
So, as expected, I had to miss two days. But I'm back! I have a bit of time this evening to carry on with closures - certainly, completing the first part of the Treehouse track. So far, it's all been fine and I haven't found any issues. OK, let's go! Start Time - 18:56 func gameCounter() -> IntegerFunction {          var localCounter = 0          func increment( _ i: Int ) {         print ( "Integer passed in: \ ( i )" )     }          return increment } So just a reminder of scope. The localCounter variable is local as it is inside the body of the method; it can't be used outside that function.  *Paused at 19:03 (not able to concentrate - need to come back to this to take it in properly.) *Continued at 20:11 The increment function now has localCounter += inside it. It can use the localCounter var as it is defi...

Angela Yu Xcode 12 Course - Part 21 (lectures 193 to 197)

Image
Yes, so I have found some sort of rhythm, hence another entry today! Just continue from where we got to.... *Ended up continuing from 4th April instead! Start Time - 17:09 Swift Closures (advanced Swift!) My understanding of these is that they are like functions without a specific name. So they can be shorter/easier etc. I tried conceptualising them before with Bob's course but it was at that point that I gave up on them! Function - bread analogy. An input with a machine that cuts up the bread, then an output (sliced bread!). Three variations on functions - 1.Those that do something without any input/parameters or have an output 2. Those that take an input with no specific output 3. Those that take an input and give an output So closures are a step beyond that! Simple function - with input, output and something that happens. Then an adding one, which we're going to use into the original function... Here's how it now looks -   Ahh so I can pa...

Bob Lee Course Part 18 (lectures 30 to 32)

Image
So a few things happening when working on, and successfully completing my first own app. I got obsessed, obsessed with getting it right - making it usable, a clear view and the hours ticked away. This was definitely a great way to learn! To branch out and take the risk of trying something that could have easily failed was no easy feat. Since the last entry, I've tweaked the view for a final time and worked out how to create my own app icons. It is not just a cliche saying it, but learning through direct practice and experience has really worked! Now I'm thinking about what 'simple' apps I could make next. It's very tempting to; various ideas have started entering my head since and during 'How Many Letters'. But I need a balance. I need to continue with Bob's course and try to understand Swift on a higher level as otherwise, my code will always be a bit limited and more verbose than it should be. My goal is to complete closures today, then decide if I w...