J. C. Rocamonde J. C. Rocamonde - 1 year ago 52
Swift Question

How can different functions have the same name?

From Apple's documentation:

Responding to Location Events

func locationManager(CLLocationManager, didUpdateLocations: [CLLocation])

Tells the delegate that new location data is available.

func locationManager(CLLocationManager, didFailWithError: Error)

Tells the delegate that the location manager was unable to retrieve a
location value.

func locationManager(CLLocationManager, didFinishDeferredUpdatesWithError: Error?)

Tells the delegate that updates will no longer be deferred.

func locationManager(CLLocationManager, didUpdateTo: CLLocation, from: CLLocation)

Tells the delegate that a new location value is available.

I have a piece of my code which looks like:


class ViewController:UIViewController, CLLocationManagerDelegate {


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {


How can I have in the same class, two functions that have the same name but are called in independent ways depending on the arguments passed? In other programming languages, afaik you can't do that.

Answer Source

In Swift, the argument names and types are part of the function name. So in your example, the functions are named differently because the arguments are different.

This is why you see the argument names included in the method names in the documentation, for example:


Also, even if the arguments are named the same, if their types are different, this is allowed. A simple example:

class Greeter {
    func greet(arg: Int) -> String {
        if (arg < 12) {
            return "Good morning!"
        } else if (arg < 18) {
            return "Good afternoon!"
        } else {
            return "Good evening!"

    func greet(arg: String) -> String {
        return "Hello, \(arg)."

In this example, you could call Greeter().greet(4) or Greeter().greet("Aaron") and control would flow to the appropriate function.