在Swift函数设计中何时应使用参数标签,何时应省略参数标签?

时间:2019-05-15 18:40:19

标签: swift

根据https://docs.swift.org/swift-book/LanguageGuide/Functions.html

我们可以将功能设计为

带有参数标签

func someFunction(firstParameterName: Int, secondParameterName: Int) {
    // In the function body, firstParameterName and secondParameterName
    // refer to the argument values for the first and second parameters.
}
someFunction(firstParameterName: 1, secondParameterName: 2)

省略参数标签

func someFunction(_ firstParameterName: Int, _ secondParameterName: Int) {
    // In the function body, firstParameterName and secondParameterName
    // refer to the argument values for the first and second parameters.
}
someFunction(1, 2)

我们应该遵循任何经验法则或最佳实践吗?这样一来,我们知道什么时候应该使用参数标签,什么时候应该省略参数标签,何时设计函数?

1 个答案:

答案 0 :(得分:1)

就Swift的命名而言,您应该签出API Design Guidelines。这些准则将使您大致了解如何在Swift中命名事物。公平的警告是,这些规则只是理论上的,最后您将决定如何在特定情况下命名函数。别太着迷于此,即使最有经验的开发人员也对此感到烦恼,所以有人告诉我。

在您的特定情况下,您绝对不应忽略参数标签,因为类型信息非常通用,并且不提供有关所传递内容的任何线索。

希望这会有所帮助!

相关问题