optional func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])
What is didUpdateLocations
? What is the reason for using such a name? I think generally, with other methods.
optional func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])
What is didUpdateLocations
? What is the reason for using such a name? I think generally, with other methods.
As @KnightOfDragon already mentioned Swift differentiates between internal and external parameter names.
Consider the following example:
class Bla : NSObject, CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}
}
let bla = Bla()
bla.locationManager(someLocationManager, didUpdateLocations: [])
didUpdateLocations
is the external parameter name that is used when calling the function. locations
is the internal one that you use in the actual implementation.
The reason for that behavior is that when calling the method you clearly know what each parameter is used for, what the function does and you can read the call like a normal english sentence:
"The locationManager someLocationManager didUpdateLocations (to) []"
On the other hand when implementing the function you do not want to have to deal with the readable name didUpdateLocations
as your variable name, but what you want to use is the locations
array.
Only having one name would produce sub-optimal results since you would either have to write
print(didUpdateLocations) // ugly variable name
or
bla.locationManager(someLocationManager, locations: [])
// what the **** is this function doing
Function Parameter Names
Function parameters have both an external parameter name and a local parameter name. An external parameter name is used to label arguments passed to a function call. A local parameter name is used in the implementation of the function.