Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

method syntax in objective-c [duplicate]

Tags:

objective-c

I'm a .NET programmer new to objective-c, and I'm struggling to understand some nuts and bolts syntax. For example, how should I parse this method signature:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath {

I understand what the "-" char means, and (UITableViewCell *) defines the return type. But the rest has me confused.

like image 244
Chris Miller Avatar asked Jul 07 '10 16:07

Chris Miller


3 Answers

(1)      (2)          (3)            (4)         (5)             (6)                 (7)       (8)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  1. "-" Defines an instance method
  2. Returns UITableViewCell pointer
  3. First part of the method signature named "tableView"
  4. Takes a UITableView pointer
  5. With the local variable name "tableView"
  6. Second part of the method signature "cellForRowAtIndexPath"
  7. Takes a NSIndexPath pointer
  8. With the local variable name "indexPath".

The actual method signature is: tableView:cellForRowAtIndexPath:.

like image 199
Sam Dolan Avatar answered Oct 01 '22 18:10

Sam Dolan


Read Apple's documentation, like Objective-C: A Primer. It's explained right there. You know, the maker (Apple or Microsoft) has a lot of documentation on their site ...

like image 40
Yuji Avatar answered Oct 01 '22 18:10

Yuji


Objective-C uses named, inline parameters for methods. (As bblum points out in the comment below, this style of parameters are sometimes called "interleaved".) This is a reflection of it's heratage as a mix of C and SmallTalk syntax. The trailing colons denote the names of the parameters to the method. For your method, the full name of the method is referred to as tableView:cellForRowAtIndexPath:. It takes two parameters, a pointer to a UITableView, and pointer to a NSIndexPath. In a java-like language, this method signature would look something like:

   public UITableViewCell cellInTableViewForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath);
like image 24
Jason Jenkins Avatar answered Oct 01 '22 19:10

Jason Jenkins