Bob Lee Course Part 18 (lectures 30 to 32)

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 want to proceed with Chapter 3. If not, I will switch to Sandra's course - a new one - at that point. OK, here we go!

Start Time - 11:54

Capture Lists


var closureArray: [() -> ()] = []

var i = 0

for _ in 1...5 {
    
    closureArray.append {
        print(i)
    }
    i += 1

}

So that is an example of a closure and I'm using a for loop to then add to the array. Presumably I'll be adding 1, then 2 etc. to the array. 

closureArray[0]()
closureArray[4]()

Interesting - whatever is printed is 5 every time! That was unexpected. 

Key point - closures are REFERENCE types. 

Bob makes the point that you need to capture or COPY. Not reference! 

var c = 7
var d = 7

let smarterClosure: () -> () = { [c, d] in
print(c, d)
}



smarterClosure()

OK, not quite sure about this but a key point in is that you need to capture/copy the values at a certain point, otherwise they will update. 

var smarterClosureArray: [() -> ()] = []

var j = 0

for _ in 1...5 {
    smarterClosureArray.append { [j] in
        print(j)
    }
    j += 1
}


smarterClosureArray[0]()
smarterClosureArray[1]()

Right so the smarter closure array is purely the bit inside the for loop. The closure created there means the values are captured rather than referenced. It means the values will basically change. 

So the closure is a 'reference to the FINAL value'. As opposed to the value in the the second closure for loop, where it is a 'capture list'. 

I can use an alias so I could put e.g. num = j in the square brackets, then put print(num). 

So that was all about 'capture lists'. It's important to have these as values as otherwise I'd be using the same reference type value all of the time!

Trailing Closures 


Right so that's an example of a trailing closure. 

So the trailing closure bit is ignoring part of the code - pressing enter and then doing something else....

Trailing closure ignores the parameter. It is when using a single closure block at the end. 

This is so confusing. Bob is really losing me here with this stuff. It's not really going in at all - so theoretical and devoid of examples or challenges. I'm going to try to complete the chapter and I think I'm done with Bob's course for now. 


The point here is that each line of code has the same functionality. The last one is clearly the most elegant. 

Bob says 'practice on your own'. But how?! How can you actually PRACTISE this stuff. You can't just get used to that syntax. Easier said than done - not very helpful. 

Right one more lesson! I'm going to have a go but don't hold out much hope. This will be the last lesson (after the next one - conclusion of course) to look at for Bob for now. 

Completion Handler

So from the introduction, my understanding is that this is using APIs - from firebase and other sites where you acquire data and information from those. 

Definition - do something when something has been done....

So notify the user when the download has been done. Is this similar to property observers I wander?

import UIKit

let firstVC = UIViewController()
let nextVC = UIViewController()

firstVC.present(nextVC, animated: true, completion: nil)

firstVC.present(nextVC, animated: true, completion: { () in
    print("Next view!")
})

Bob then shows simpler versions including the use of trailing closure block (getting rid of the completion part onwards). 


So uses of the another closure:



And a simpler version:



Stopped at 12:46 (52 minutes total)

This is seriously a waste of time. Bob has made such a huge conceptual leap in this chapter that there is no point plodding along with it. I tried to practise and learn the syntax of closures before but it didn't really help. Here's the deal - I will come back to Bob's course and go over ALL of closures again, once I have a better understanding of the basic syntax. But for now, I need to try out another course. Usual pattern repeating in the sense that I can only get so far with any course! No matter, it's good to recognise when it's time to move on!

Now I'm going to update the 'sticky' list of the summary of what I've been doing. Then to start Sandra's course!

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)