user2202911
user2202911

Reputation: 3014

What is the extra variable in this Swift function declaration

This line is from a tutorial I'm following, take a look at the second parameter. Coming from another language, the 'cellForRowAtIndexPath' is unexpected. What is the purpose of that variable(in terms of the Swift language, not the iOS framework) & what is the concept of this "extra variable" called so I can do further personal research?

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

Upvotes: 1

Views: 66

Answers (2)

Simon
Simon

Reputation: 2469

See the following code:

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

from: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html (section External Parameter Names)

cellForRowAtIndexPath is the name you see from outside the function, so when calling it

indexPath is the name inside the function, usually a shorter one.

NSIndex is the type

Upvotes: 0

Incognito
Incognito

Reputation: 16577

Yes it can be odd if you are coming from Java or C# and I guess basically from any other language except Objective-C :) In Swift there is a concept of external and internal parameters. In your example cellForRowAtIndexPath is external name which is 'visible' to the method caller and 'indexPath' is internal or local name which is used inside the method implementation.

Upvotes: 2

Related Questions