Swift中方法前的参数名称是什么?
What is the name of the parameter before the method in Swift?
optional func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])
什么是didUpdateLocations
?使用这样一个名称的原因是什么?我认为一般,用其他方法。
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.
正如@KnightOfDragon 已经提到的 Swift 区分内部和外部参数名称。
考虑以下示例:
class Bla : NSObject, CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}
}
let bla = Bla()
bla.locationManager(someLocationManager, didUpdateLocations: [])
didUpdateLocations
是调用函数时使用的外部参数名称。 locations
是您在实际实施中使用的内部。
这种行为的原因是,在调用该方法时,您清楚地知道每个参数的用途、函数的作用,并且您可以像普通英语句子一样阅读调用:
"The locationManager someLocationManager didUpdateLocations (to) []"
另一方面,在实现函数时,您不希望将可读名称 didUpdateLocations
作为变量名处理,但您想要使用的是 locations
数组。
只有一个名字会产生 sub-optimal 结果,因为您要么必须写
print(didUpdateLocations) // ugly variable name
或
bla.locationManager(someLocationManager, locations: [])
// what the **** is this function doing
optional func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])
什么是didUpdateLocations
?使用这样一个名称的原因是什么?我认为一般,用其他方法。
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.
正如@KnightOfDragon 已经提到的 Swift 区分内部和外部参数名称。
考虑以下示例:
class Bla : NSObject, CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}
}
let bla = Bla()
bla.locationManager(someLocationManager, didUpdateLocations: [])
didUpdateLocations
是调用函数时使用的外部参数名称。 locations
是您在实际实施中使用的内部。
这种行为的原因是,在调用该方法时,您清楚地知道每个参数的用途、函数的作用,并且您可以像普通英语句子一样阅读调用:
"The locationManager someLocationManager didUpdateLocations (to) []"
另一方面,在实现函数时,您不希望将可读名称 didUpdateLocations
作为变量名处理,但您想要使用的是 locations
数组。
只有一个名字会产生 sub-optimal 结果,因为您要么必须写
print(didUpdateLocations) // ugly variable name
或
bla.locationManager(someLocationManager, locations: [])
// what the **** is this function doing