Angela Yu Xcode 12 Course - Part 15 (lectures 167 to 168)

As I mentioned enough times already, this is a tough chapter! Before, when I attempted it (when was that, October/November?) I actually bailed on it. I felt the need to go over the fundamental coding concepts again and that the Clima project was too 'out there'. However, I'm looking at it differently now - it's a great opportunity to see something completely different! And there are some good bits along the way!

Start Time - 17:55

Delegates and Protocols

I've seen these before - it's about modelling behaviour and about getting something else to do a job essentially!

In the context of Clima, the CLLocationManager is the delegate to do the job of looking for the location (for the location manager).

Right so seeing it another way - a protocol is a contract! We have drawn up a contract - if it wants to be the delegate, then it needs to be able to handle what is in the protocol created -

protocol ChangeCityDelegate {
    func userEnteredACityName(city: String)

}

Right so a bit like a builder to build a house - the builder needs certain qualifications e.g. being able to lay bricks. So in this case, the protocol says that the method above needs to be used.

OPTIONAL CHAINING - 

   delegate?.userEnteredACityName(city: cityName)

Right that explains that better as I always thought that optional chaining was multiple lots of optional binding! But actually it's using the ? and the . syntax - if there is a value used there, then the cityName is used as the city. And if not then no value is used. 

Paused at 18:12 (17 minutes so far)

So some good stuff so far. Clarified a couple of things - protocols and optional chaining! Next up it's going to be about passing data between view controllers....

Restart at 20:25

Passing info between View Controllers

So below we have one view controller


And some code is needed....



An extra line at the bottom - 


Right we have as well the segue function code needed - 


And the data needs to be sent back again!




Each view controller has various bits of code - no point doing any more screen shots of it to be honest. 

Right so to make sense of all of this, the two screens basically had data being passed back between themselves. That's fine. Protocols are just coming into this now. This is like a CONTRACT - so the outline of the contract is set in the protocol. 

The protocol itself DOES NOT know how to actually do any of what it sets out. It instructs but does not actually do it!




So then we have the first view controller conforms to the CanReceive protocol. The delegate method needs to be put inside the view controller too. 

Creating a variable called delegate that conforms to the CanReceive protocol....

This is all so complicated! But like I said, baby steps! I don't need to KNOW all of this straight away. It's fine to be exposed to it and soak it in. 

*I just thought of something - this whole work on passing info between controllers can be used for HowManyLetters - for recording high scores! This could tie in perfectly. Anyway, that will be the next potential project to work on, once I've done the rest of this Angela chapter and the next - but will not do so INSTEAD of Angela's work!

Finish Time - 20:52 (Total today - 44 minutes)

Again, plenty to be proud about. I'm not making huge strides or conceptual leaps but making small, steady little steps. It will not all sink in at once - these advanced concepts will take time. Applying it to the HowManyLetters will be the best way to have some real context. Just a couple more chapters then Clima is done!

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)