这个 Swift 函数声明中的额外变量是什么

What is the extra variable in this Swift function declaration

这一行来自我正在关注的教程,请看一下第二个参数。来自另一种语言的 'cellForRowAtIndexPath' 是出乎意料的。该变量的目的是什么(就 Swift 语言而言,而不是 iOS 框架)以及这个 "extra variable" 的概念是什么,以便我可以进行进一步的个人研究?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

是的,如果您来自 Java 或 C#,我想基本上来自 Objective-C 以外的任何其他语言,这可能很奇怪 :) Swift中有外参数和内参数的概念。在您的示例中,cellForRowAtIndexPath 是外部名称,它是方法调用者的 'visible','indexPath' 是在方法实现内部使用的内部或本地名称。

见以下代码:

func someFunction(externalParameterName localParameterName: Int) {
    // function body goes here, and can use localParameterName
    // to refer to the argument value for that parameter
}

来自:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html(外部参数名称部分)

cellForRowAtIndexPath是你从函数外部看到的名字,所以调用它的时候

indexPath是函数内部的名字,一般比较短

NSIndex 是类型