3

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:

ViewController.swift

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.

3 Answers3

2

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:

locationManager(_:didUpdateLocations:)
locationManager(_:didFailWithError:)
locationManager(_:didFinishDeferredUpdatesWithError:)

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.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • And does that include the argument types as well? –  Aug 20 '16 at 15:29
  • And so in the example you provide, I guess the "_:did..." part is what identifies the function, right? –  Aug 20 '16 at 15:30
  • 1
    Yes to both. I updated with an example. – Aaron Brager Aug 20 '16 at 15:32
  • And what would happen in case the arguments and their types are the same but the output/content is different? which function would be called? Would there be a runtime error? –  Aug 20 '16 at 15:35
  • 1
    If the argument names are the same *and* the argument types are the same, the code will not compile. If the types are different (eg String vs. Int), the function will get called depending on the type you pass in. – Aaron Brager Aug 20 '16 at 15:36
  • thank you, I missed adding _and the argument types_ even though it was my intention. Now I understand. –  Aug 20 '16 at 15:39
0

It's called "function overloading" (or "method overloading"), having multiple functions/methods with the same name that are distinguished by their signature (the number and types of their arguments). It's very common in programming languages, including C++, Java, C#, and apparently Swift.

How overloading is handled under the covers varies quite a bit by language. For example, last I looked (a long time ago), C++ used name mangling — the actual name of the function was created by taking the base name and adding things to it to indicate the types of the arguments. (Very early on, C++ was a pre-processor that output C code.) Java doesn't need to do that because it was built with overloading in mind. But at the high level where we deal with them, the functions have the same name.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • It's interesting for me since I come from Python and haven't learned neither C++, nor Java nor any of those but Swift, and it really was surprising for me. –  Aug 20 '16 at 15:37
  • 1
    @J.C.Rocamonde: :-) Yeah, you can't do overloading in languages with dynamic or loose typing like Python or JavaScript. – T.J. Crowder Aug 20 '16 at 15:46
-1

Function overload. c++, can do that.


edit:

ok. i added more details. sorry, I am new in programming. i am afraid of giving wrong answer.

func foo1(_ x: Int) {
    print("the argument is Int")
}
func foo1(_ x: Double) {
    print("the argument is Double")
}

// it depends the type of the value to pass to the function.
foo1(3) // print "the argument is Int"
foo1(4.7) // print "the argument is Double"

// when you assign the function to a variable,
// and there are other functions with the same name and same parameter names, 
// then, you have to tell the type of the function explicitly.
let pToFunc1: (Int) -> Void = foo1

// but normally, you can assign it without telling the type.
func foo2(x: Int){}
let pToFunc2 = foo2

// but same function name with different parameter names,
// when you assign this function, you need to tell the parameter explicitly.  
func foo3(x: Int){}
foo3(x: 4)
func foo3(y: Int){}
foo3(y: 5)
let pToFunc3 = foo3(x:)
Carl Hung
  • 545
  • 4
  • 17
  • 3
    This answer does not add anything that hasn't been said before, and it's very vague as well. You should consider deleting it or adding some extra information to enrich the answer. –  Aug 20 '16 at 15:34
  • 1
    Good explanation. I didn't know about the last part you said. –  Aug 20 '16 at 17:53
  • please up-vote me, that is sad that i tried to answer and got down-voted for a novice(me). this is the first or second time i tried to answer. – Carl Hung Aug 20 '16 at 17:59
  • I alreadady did, you got still 3 more downvotes –  Aug 20 '16 at 18:02
  • if i don't remove, will it affect the reputation on my profile? – Carl Hung Aug 20 '16 at 18:08
  • thanks. i remove it now and repost it again – Carl Hung Aug 20 '16 at 18:09
  • while this answer describes correctly method overloading, it doesnt answer the question, as the methods given in the example are different method altogether. – vikingosegundo Aug 21 '16 at 00:31