Apple Swift:这个函数的第二个参数是什么意思

时间:2014-06-30 00:39:00

标签: swift

在以下功能中:

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return 10
}

是通过以numberOfRowsInSection为参数调用函数section返回的结果的第二个参数?如果是这样,它从哪里获得section

2 个答案:

答案 0 :(得分:1)

此语法允许为参数设置一个名称,该名称与用于捕获该参数值的局部变量的名称不同。

  • numberOfRowsInSection是调用此函数时使用的参数的名称。
  • section是保存传递值的局部变量的名称。

所以你会像这样调用这个函数:

Int rowCount = tableView(tableView: aTabelView, numberOfRowsInSection: 10);

在该函数中,您将使用如下参数:

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return section;
}

答案 1 :(得分:0)

numberOfRowsInSection是调用函数时的参数名称。 section是函数中的参数名称。因此,例如,您可以按如下方式调用此函数:

myObject.tableView(aView, numberOfRowsInSection:4)

但是从函数中,您将引用4,如下所示:

let valueOfSectionArgument = section