Treehouse Intermediate Course - Part 16 (Delegation 3)

Here we are now! So, first of all, I've spent several hours tinkering with the F1 Sim Playground and made some major breakthroughs! Yesterday, I realised that the main issue was that the random value was being added to ALL the drivers' distanceTravelled. This basically negated the whole point of the random element. Essentially, the maxSpeed would determine the whole race! So I came up with a solution...eventually! And I am so proud of it! I can now run a sim for a general race for as many drivers as I want, all with their own maxSpeed set, and the results are randomised, but based mostly on what their maxSpeed is. Cool!

Unfortunately, today, I have been battling with trying to run the overall results. I tried it as a for loop, as a closure...a function...and I've had to give up! Always frustrating...actually kind of heart-breaking but that's where resilience needs to come in. So easy to feel disappointed but this is where I need to get more ruthless. I don't have the solution, I can't find the solution so I have to move on. The F1 Sim still exists and there's loads more I want to do, but I'm happy to leave it there on pause for now. It's more important to keep learning - I want to complete the delegation course before having another crack at it with corners, car advantages, circuit specifications...there's plenty of potential. Another factor is my eyes and head; it hurts to use the laptop too much. And I've used it plenty since yesterday evening! So a quick bit of Treehouse learning then NOTHING else today! 

Start Time - 16:36

CLLocationManagerDelegate

OK a bit of faff with downloading the project file. Pasan doesn't make it clear/have the right file int the teacher's notes bit. Anyway, I have it now!

Asynchronous method - that means the code doesn't get executed immediately. It could be one second or ten minutes! This makes sense as on your phone, you can accept or deny access to location settings etc.

The CLLocationManager class uses a delegate pattern. You can search in the Swift library via Xcode for this; all the different functions etc. that can be accessed.



In Objective C, a protocol could have required and optional methods. So the class we are using needs to be an objective C class as we are inheriting from it....

We are inhering from NSObject. This may have changed in more recent Swift!

Swift does not have the concept of optional protocols.

Here's ALL of the code -

import Foundation
import CoreLocation

class LocationManager: NSObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()
    
    override init() {
        super.init()
        
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            manager.requestLocation()
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print(locations.first!)
    }


}


This is similar to what I learned on one of Angela's (admittedly, sorry Angela!) tricky projects.

Something called a callback. OK to summarise, we have conformed to a particular delegate which needs to contain 3 functions. These are all in the Swift online info via Xcode that I mentioned! As we need all three to follow the protocol - to use that delegate - then all of the functions need to now be fleshed out.

This is just an example of how the delegate pattern is used basically. I'm getting more used to the idea of delegation!

UITextFieldDelegate

Right, last video for today before the challenge! This time we have a text field and label set up.

When you use a text field, or form, you immediately start getting more results - and more get filtered as you go! So a text field uses the delegate pattern; it can inform us - each letter entered will give an event back.

OK so I get the example. To get the characters coming up, it needs to conform to this protocol.

Quiz - 3 out of 3!

So that's all done! Delegation complete! I'm getting the idea of it, even if I'm not putting it all into practice just yet. Next course IS  a PROJECT! And it looks interesting - make your own story! Again, I've done this with Angela, which was a great course. The only thing with hers was that it seemed quite messy with the array of different parts. We'll see how that goes when I have a look tomorrow. Before I finish, I'm just going to see how easy it is to answer people's questions on the Treehouse Community for the beginner courses...

OK there aren't any!

Finish Time - 17:09 (33 minutes)

A good amount of time, just to finish off delegation. I am getting the hang of it, even though it's a complex concept still. More practice and I'll get it better! Next time - tomorrow morning most likely - I'll have a crack at the story app. It will probably be outdated and may not even work but I'll certainly give it a go. I'm at breaking point for my eyes and brain today so time to sign off. 


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)