-2

I have questions about this statement:

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

internal is the scope?

func is a keyword that declares method/functions?

tableView is the name of the method?

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?

Is UITableView a parameter type?

Is IndexPath a parameter type?

What is indexPath in cellForRowAt indexPath? Why are there 2 words?

UITableViewCell is the return type, right?

So confused, thank you so much for your help!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Marcus Kim
  • 283
  • 2
  • 12
  • 5
    Nothing really wrong with this question, but if you're having trouble with the language at this level you'd do well to seek out more introductory material, like [The Swift Programming Language](https://docs.swift.org/swift-book/). – rickster Jul 19 '18 at 20:21
  • Please read the [Functions](https://docs.swift.org/swift-book/LanguageGuide/Functions.html) chapter of the Swift book (and the rest of the book too). – rmaddy Jul 19 '18 at 20:56

2 Answers2

2

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 function 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.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • This answer is partially incorrect. The name of the function is not `tableView`. The name of the function is `tableView(_:cellForRowAt:)`. – rmaddy Jul 19 '18 at 20:55
  • As a certain Bard [once implied](https://en.wikipedia.org/wiki/A_rose_by_any_other_name_would_smell_as_sweet), "name" is a fluid concept. :) Edited to clarify base name vs full name. – rickster Jul 19 '18 at 21:57
1

You are correct about internal and func and tableView. The underscore is there because you do not have to include the parameter name when calling the function. You can do:

tableView(myTableView, cellForRowAt: someIndexPath)

Instead of:

tableView(tableView: myTableView, cellForRowAt: someIndexPath)

UITableView is the parameter type same with IndexPath. When the function is called (as I showed above) you use the first word cellForRowAt however internally the function will use indexPath. The two words are there for readability when the function is called. UITableViewCell is the return type.

Tob
  • 985
  • 1
  • 10
  • 26
  • 2
    All true, though "you" (typically) never call this function, you just implement it. In delegate protocols, UIKit calls you! – rickster Jul 19 '18 at 20:19
  • That is a good clarification point @rickster – Tob Jul 19 '18 at 20:22
  • This answer is partially incorrect. The name of the function is not `tableView`. The name of the function is `tableView(_:cellForRowAt:)`. – rmaddy Jul 19 '18 at 20:54