Sean Sean - 1 month ago 10
Swift Question

When calling a function in Swift, what does each part mean?

For example, if I am writing:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)


what does
tableView
,
_tableView: UITableView
, and
didSelectRowAt indexPath: IndexPath
each refer to in general terms?

Answer

The first tableView is the method name. Then, inside the parentheses are parameters, separated by commas. Each parameter has three parts: the external name, the internal name, and the type. If the external name (the name of the parameter used by code calling the method) is the same as the internal name (the name of the parameter used by code inside the method), you can replace it with an underscore (_). So, the first parameter has an internal and external name of tableView. The part after the colon (:) is the type, the first parameter has a type of UITableView. The second parameter has an external name of didSelectRowAt, an internal name of indexPath, and a type of IndexPath.