什么'使用'在Swift方法中做什么?

时间:2017-06-15 18:29:20

标签: swift

我注意到,除非我这样做,否则我会收到错误

animateTransition(using transitionContext: UIViewControllerContextTransitioning)

然而,有些教程将此方法表示为

animateTransition(transitionContext: UIViewControllerContextTransitioning)没有using

如果我加入using似乎只会构建,但我很好奇它的角色以及何时发生了变化。

1 个答案:

答案 0 :(得分:3)

检查apple documentation

  

指定参数标签

     

在参数名称前面写一个参数标签,用a分隔   空间:

func someFunction(argumentLabel parameterName: Int) {
    // In the function body, parameterName refers to the argument value
    // for that parameter.
}
     

这是greet(person:)函数的变体,它带有一个   人的名字和家乡,并回复问候:

func greet(person: String, from hometown: String) -> String {
    return "Hello \(person)!  Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// Prints "Hello Bill!  Glad you could visit from Cupertino."
     

使用参数标签可以允许在一个函数中调用一个函数   富有表现力,类似句子的方式,同时仍然提供一种功能   意图可读且清晰的身体。