Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Blog Reader iPhone App Exploring the Master-Detail Template Modifying The Master-Detail Template

Objective C Function Syntax

I come from a C++ and C# background. Can someone break down the syntax of this function in this video:

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

I understand it returns a NSInteger and takes another NSInteger as a parameter but I don't quite understand the tableView part of it: "tableView:(UITableView *)tableView" before the function name. it's not a return type so I find the placement of it weird. Does it have to do with inheritance? Can someone break down this syntax for me please. Thanks.

2 Answers

John Wheal
John Wheal
27,969 Points

In C, the syntax would be:

NSInteger tableView(tableview, section);

Objective C adds more words than C. In the example below:

  • numberOfRowsInSection describes the input parameter to the method
  • NSInteger is the type of input parameter
  • section is the actual parameter

Although it looks long and complex it is actually much more readable the C.

By your C syntax example, you're indicating the name of the method is tableView. But I understand the name of the method is numberOfRowsInSection. I don't get it how there is a parameter before the method name i.e. tableview. What makes it special that it can go before instead of after the method name like I'm used to seeing in C? Thanks.