0

I am new to Swift and working through an online iOS development bootcamp course and the course has introduced the delegate design pattern. The course earlier demonstrated the design pattern via the use of the UITextFieldDelegate and is now introducing an application that requests the user's location via import CoreLocation, establishing a ViewController as a CLLocationManagerDelegate, and then responds to the didUpdateLocations method.

The course suggests implementing delegate methods as extensions of the particular ViewController class. The below extensions work, but I am trying to understand the difference between the declaration of the delegate methods:

import UIKit
import CoreLocation

class myViewController: UIViewController {

   [...]

}

extension myViewController: UITextFieldDelegate {

   func textFieldShouldReturn(_ textField: UITextField) -> Bool {
      [...]
      return true
   }
    
   func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
      [...]
      return true
   }

   [...]

}

extension myViewController: CLLocationManagerDelegate {
    
   func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      [...]
   }
    
   func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
      [...]
   }
    
}

The UITextFieldDelegate extension has distinct method names. How is it possible for the methods required by the CLLocationManagerDelegate protocol to both be named locationManager?

  • 1
    In Swift a function isn't identified only by the function name, but by the whole function signature; that is the name, its argument names and their types and its return type so `func someFunc(value: Int)->Int` and `func someFunc(value: String)->String` are different functions – Paulw11 Jul 02 '22 at 22:56
  • Interesting, I couldn't find that in the Swift documentation but I'm sure it's in there somewhere. Thanks! – mikstravaganza Jul 02 '22 at 23:56

0 Answers0