Let's create a simple class that has a location manager, which will receive location updates:
class LocationAware: NSObject {
let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
}
}
Then, we need to mark LocationAware as being CLLocationManagerDelegate. Delegation uses protocols to ensure the methods that may be called are properly implemented on the delegate:
extension LocationAware: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// do something with the updated location
}
/* more delegation methods */
}
Now, with that example in mind, let's create our own delegation pattern that will facilitate communications between two objects.