Bob Lee Course Part 16 (lectures 26 to 28)
Back to Bob! Now, I have genuinely been ill for the last few days - headaches, sore throat, cold...flu but really overwhelming. Not so bad to be off work but pretty horrible. Anyway, I have some time at last! I forgot to post the first (proper) Xcode project, so that's now several days out of sync. Not to worry. I will add to that later if I can! Now, the plan is to continue with Bob's Intermediate course, with the possibility of switching to Sandra's after chapter 3. Realistically, that's 2 main entries of the content with one consolidation one. Let's see how it goes!
Start Time - 17:15
CHAPTER 3!
Bob makes the point that people usually struggle with closures. Good - because I always have!
Introduction to Closures (1)
So the key point with closures is that they are essentially functions without using the 'func' keyword.
You can also create a variable and assign it to a function, then it has the same functionality. Cool!
Start Time - 17:15
CHAPTER 3!
Bob makes the point that people usually struggle with closures. Good - because I always have!
Introduction to Closures (1)
So the key point with closures is that they are essentially functions without using the 'func' keyword.
You can also create a variable and assign it to a function, then it has the same functionality. Cool!
var addClosure: (Int, Int) -> Int = { (number1: Int, number2: Int) in
return number1 + number2
}
So in that example, I've basically done the same job of an adding function.
Bob shows how you can strip away more so the code is simpler:
var addClosure2 = {
(number1: Int, number2: Int)
in return number1 + number2
}
addClosure2(5, 6)
var addClosure3 = {
(number1: Int, number2: Int) in
number1 + number2
}
I think I know what's coming - the $0 and $1 eventually making it very simple!
var addClosure4: (Int, Int) -> Int = { $0 + $1 }
Yep, thought so!
Bob makes the point here where you don't HAVE to name the parameters. Using 'number1, number2' is actually unnecessary. With closures, as long as the code is clear then parameter names are basically not needed.
So an example of comparing using a function and a closure:
So I don't see the point of doing the closure there.
Again, the build up of more complex to simpler! A key point here is the keyword 'return' coming out. Swift can figure out this.
Closures can be used in various forms - can be simpler than functions.
Introduction to Closures (2!)
OK key definition time - a function is a GLOBAL closure.
Interesting general point - which I know already - the best way of learning is not only practice, but through your own practice. You learn so much more when it's a challenge that you have set for yourself. This is a key point - when I do more Xcode project practice, it will help everything click together. Understanding the 'why' part is much more useful than the 'how'.
I disagree to an extent though - Bob should set the occasional problem, just to see if the 'why' has been understood. Otherwise it is all a bit passive.
func returnClosure() -> ((Int, Int) -> Int) {
return addClosure
}
let returnClosureBlock = returnClosure()
returnClosureBlock(4, 5)
OK I get it so far. The return bit is all in parentheses.
func returnClosureDirectly() -> ((Int, Int) -> Int) {
return { (number1: Int, number2: Int) in number1 + number2 }
}
I can actually get rid of the Int bits in the return line...
func returnClosureDirectly() -> ((Int, Int) -> Int) {
return { (number1, number2) in number1 + number2 }
}
Interesting, having the Ints in didn't allow the constant creation of the same function!
So simpler still:
func returnClosureDirectlyTwo() -> ((Int, Int) -> Int) {
return { $0 + $1 }
}
Pass closures above. Not too sure about these!
Another interesting example:
let example = Array(1...100).filter { $0 % 2 == 0 }
for i in example {
print(i)
}
I did the loop bit myself - remembered that was the best way to print items from an array!
Finish Time - 18:33 (1 hour 15 minutes total)
That will do for now - phew! Complicated stuff! It's clear that I need to - to be fair on Bob's recommendation - some interim practice before cracking on. So, I will look into basics of closures and how they differ from return and pass closures. Tricky stuff here but it is good to be stretched and challenged! Before doing some interim work, I'm going to have another crack with the project. Little by little!
Comments
Post a Comment