internal
is the scope?
This is an access modifier. It means that only code in the same module can see/call this symbol.
internal
is implicit if you leave it off, and has meaning primarily for when you're making frameworks — an app that links a framework can see only the framework's public
symbols, but code within the framework can see other internal
symbols in the framework. (And code in one type within the framework can't see private
symbols inside another type.)
func
is a keyword that declares method/functions?
Yes. In Swift, a "method" is just a func
tion declared in the scope of a type (class, struct, protocol, enum).
tableView
is the name of the method?
Yes... in one sense. It's what is often called the "base name" of a function/method — the part before the open-parenthesis (
. The full name of the method, which people use when trying to unambiguously refer to this method versus others, includes all the argument labels: tableView(_:cellForRowAt:)
. (Even a full name isn't unambiguous in Swift, though, because functions/methods can be overloaded on parameter/return types.)
Usually a function name is a verb phrase, describing the action performed by a function or characterizing the result, but there's a longstanding convention otherwise in delegate/datasource protocols. (And this method is from the UITableViewDataSource
protocol.) In such protocols, the "base name" of the function to name the object that's delegating some action.
What is _tableView
? Is this the name of a parameter? If so, why do we need the underscore when cellForRowAt
does not have an underscore, assuming cellForRowAt
is also a name?
It's not _tableView
, it's _ tableView
— note the space. The underscore generally means "ignore this" in Swift. In this case, the underscore takes the place of an argument label, meaning that the caller of this method doesn't write one for the first parameter:
let cell = tableView(myTableView, cellForRowAt: selectedIndexPath)
^ nothing here, just the value being passed as an argument
Is UITableView
a parameter type?
Is IndexPath
a parameter type?
Yes.
What is indexPath
in cellForRowAt indexPath
? Why are there 2 words?
cellForRowAt
is an argument label. indexPath
is the internal (to your implementation) name of the parameter.
Argument labels are used by whoever calls your function — e.g. a call site for this looks like:
let cell = tableView(myTableView, cellForRowAt: selectedIndexPath)
Parameter names are how you refer to that value within the function body:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
self.doSomethingWithCellData(indexPath)
...
}
UITableViewCell
is the return type, right?
Right. If you don't include a return
statement in your implementation of this method, or return
anything that isn't a UITableViewCell
, the compiler gets upset.