Return to sender - "from a method"?

Hi,

When explaining ‘return’ in iOS Apprentice 2 Checklists, the author writes the following:

 You use  return  to send a value from a method back to the method that called it

It is not easy for me to understand what it means.
Are there two methods in one method???

For example,

override fun tableView(tableView: UITableView, 
                                       numberOfRowInSection section: Int) -> Int {
    return 1
}

which one is the method that sends the value 1 and which one is the method that calls it?

(btw, these series of iOS apprentice are THE best of all iOS tutorials. )

Suppose we write this:

func method1(param: String) -> Int {
  // do something with the String and return an Int
}

func method2() {
  let result = method1("hello")
  print(result)
  // do other stuff as well
}

Here, method2 calls method1. When method1 is done, it returns an Int value back to method2, which then prints that value.

In the case of tableView(numberOfRowsInSection), this method gets called by some other method in UIKit. You have have no control over that. Whenever UIKit decides it wants to redraw the table view, it calls tableView(numberOfRowsInSection) because it needs to know how many rows go into the table view. But that’s totally up to UIKit to decide how and when to do this.

Does that make sense?

Hi hollance,

Great!! thank you so much for your wonderful explanation.

so, method2 in the example of tableView(numberOfRowsInSection is “some other method” handled by UIKit which is invisible and something I do not have to worry about. :slight_smile:

now I understand where the return vale of items.count goes.

so, this would explain why if you used a print to debug your output/return you would see it more than once (4times for me.
I tested and with a simple tableview it does return of 1 and a print it gets called 4 times

make sense?

Sure. Every time the table view needs to know how many rows there are, it will ask the data source. It’s quite likely therefore that it will send the number of rows in section message more than once.