ManInTheArena
ManInTheArena

Reputation: 89

Method names in Objective-C are the same.

My ViewController Object must implement required Table View methods so it can act as a data source. From UITableView.h:

@required  
-(NSInteger)tableView:(UITableView *)tableView
                       numberOfRowsInSection:(NSInteger)section;
-(UITableViewCell *)tableView:(UITableView *)tableView
                       cellForRowAtIndexPath:(NSIndexPath *)indexPath;

This is a little confusing.

What is the first parameter, (UITableView *)tableView, and why does it have the same name as the method? It looks like a pointer to the TableView object. Why give it the same name as the method?

Secondly, I understand that while this appears like overloading, there are actually two methods, tableview:numberOfRowsInSection, and tableView:cellForRowAtIndexPath. Why is this?

The links below were helpful. Any additional help would be appreciated here, especially about the naming conventions.

Method overloading in Objective-C?
Is function overloading possible in Objective C?
How do I pass multiple parameters in Objective-C?

Upvotes: 3

Views: 545

Answers (3)

Nathan Day
Nathan Day

Reputation: 6037

There is no overloading because the method names aren't the same they are

tableView:numberOfRowsInSection:
tableView:cellForRowAtIndexPath:

including the :, the : is important and part of the name,

The arguments are the part after the :'s, the part before the :'s is part of the method name, method name are arguments are intertwined.

So your two method have two arguments, the first being the table view that is invoking the method.

Once you understand this, naming the argument after the part of the method name it follows is quite natural.

Upvotes: 3

icodebuster
icodebuster

Reputation: 8964

Objective-C methods are designed to be self documenting, and they borrow from the rich tradition of Smalltalk.

I'll try to explain what you have here, - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section.

  • - (NSInteger)
    This first portion indicates that this is an Objective C instance method that returns a NSInteger object. the - (dash) indicates that this is an instance method, where a + would indicate that this is a class method. The first value in parenthesis is the return value of the method.

  • tableView:
    This portion is a part of the message name. The full message name in this case is tableView:numberOfRowsInSection:. The Objective-C runtime takes this method information and sends it to the indicated receiver. In pure C, this would look like
    NSInteger tableView(UITableView* tableView, NSInteger section). However, since this is Objective-C, additional information is packed into the message name.

  • (UITableView *)tableView
    This portion is part of the input. The input here is of type UITableView* and has a local variable name of tableView.

  • numberOfRowsInSection:
    This portion is the second part of the message name. As you can see here, message names are split up to help indicate what information you are passing to the receiver. Thus, if I were to message an object myObject with the variables myTable and mySection, I would type in Objective-C style:
    [myObject tableView:myTable numberOfRowsInSection:mySection];
    as opposed to C++ style:
    myObject.tableView(myTable, mySection);.

  • (NSInteger)section
    This is the last portion of the input. the input here is of type NSInteger and has a local variable name of section.

Upvotes: 7

Michael Dautermann
Michael Dautermann

Reputation: 89509

Apple (and other SDK developers) frequently name their variables being passed the same names as the API parameters. To some folks, it just makes readability a little easier.

I mean, for the UITableViewDataSource methods, the first parameter is named for the table view for which we're getting the data for. And the variable passed into this method is also named "tableView", so if the object serving as a data source has more than one table to serve up data for, that object will know which specific table is doing the requesting.

This happens a lot in delegate methods. Take a look at the UITextFieldDelegate methods, for example. When you have more than one text field on a view, you sometimes need to know which specific text field is being typed into and/or worked with by the user.

Or, let me try to give a practical example: suppose we have a single view with two separate tables. One for hotels and another table for restaurants. Both tables pull data off some object which hosts the data (e.g. maybe a class that works with CoreData?). When "tableView: cellForRowAtIndexPath:" is called on that data source object, the first "tableView" parameter will contain the address of either the hotel table or the restaurant table, and through that, you'll know which data to return back.

Upvotes: 2

Related Questions