6

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.

  • objective-c doesn't have methods it "sends messages" to functions, there is an important semantic difference. One example is you can't send a message to NIL safely, but you can't call a method on a null pointer to an object or a null reference to a function –  Jul 07 '10 at 18:09
  • @fuzzy lollipop: Actually, you send a message to an *object* which then invokes a method. You do not send messages to functions. You can safely send a message to nil. – JeremyP Jul 08 '10 at 07:51
  • that is what I meant ;-) –  Jul 09 '10 at 01:56

6 Answers6

11
(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:.

Sam Dolan
  • 31,966
  • 10
  • 88
  • 84
  • 1
    Objective-C does not have named parameters. – bbum Jul 07 '10 at 17:13
  • @bbum: Yes I know, I've updated my post to say "part of the method signature" rather than "argument named" to avoid confusion. Thanks for pointing that out. – Sam Dolan Jul 07 '10 at 17:44
  • Thanks! It is a horse I beat upon because I've seen too many people get all confused when they try to "drop a keyword" or "named parameter" and then not have their code compile. – bbum Jul 07 '10 at 18:28
  • Is it right that it would be correct to propose an alternative at this method by writing : - (UITableViewCell *)cellForRowAtIndexPath:(UITableView *)tableView IndexPath:(NSIndexPath *)indexPath ?? I get totally confused by the fact that the method is partially called tableView... Coming from C#, I wouldn't write a method called void MyClassMyMethod... I would simply write MyMethod... Thanks for your great answer btw ! – Andy M Jul 19 '12 at 19:03
  • 1
    @AndyM: I feel your pain, but you can't do that because it would change the signature of the method that the protocol relies on. – Sam Dolan Jul 19 '12 at 23:02
  • @sdolan : :D Thank you! I understand it changes the signature, but imagine i would create a new method, i would be syntaxily correct to write up something like that ? – Andy M Jul 20 '12 at 05:12
  • 1
    @AndyM: Yes you can, but (and I'll reiterate) it won't be called by the called by the native protocols. – Sam Dolan Jul 20 '12 at 18:27
  • @sdolan : yeah got it, it was to make sure :) i find this kind of notation quite disturbing at first glance but not so bad... Just have to get used to it and i wont be able to get back to more standard java like method! Thanks for your help! – Andy M Jul 21 '12 at 18:15
4

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);
Jason Jenkins
  • 5,332
  • 3
  • 24
  • 29
  • 3
    Objective-C does not have named or keyword parameters, they are called interleaved arguments. It is an important distinction. Named or keyword implies that you can drop parts of the name. Not true. – bbum Jul 07 '10 at 16:59
  • @bblum - I never said that it used keyword parameters a la Python's keyword arguments. I mean that they are named in the sense that there is a name associated with the parameters positionally. I was trying to use descriptive terminology that would be more understandable to someone unfamiliar with the language. I'll edit the answer to include your terminology though. Thanks. – Jason Jenkins Jul 07 '10 at 18:05
  • Thanks. Both "named" and "keyword" are a source of confusion, hence the insistence upon avoiding 'em. (I can't tell you the number of times I've taught an ObjC class where a student was confused because `setObject:forKey:` was not the same as `setObject:` or `forKey:setObject:`) – bbum Jul 07 '10 at 18:31
4

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 ...

Yuji
  • 34,103
  • 3
  • 70
  • 88
2

Every foo:(bar)baz defines a parameter, for example

- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
           delegate:(id)delegate
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString *)otherButtonTitles, ... {

defines a method with five* parameters.

The stuff before the : is part of the name of the method. In this example, the method's name is

initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:

The stuff between the (…) is the type of that argument. Here, we see that the first argument must be an NSString*.

Finally it's the name of the parameter.

(*: Sometimes there is sometimes a , ..., like in here, indicating it's a variadic method.)

The method is called in the syntax

id result = [theAllocedAlertView initWithTitle:@"title"
                                       message:@"message"
                                      delegate:someDelegate
                             cancelButtonTitle:@"cancel button title"
                             otherButtonTitles:@"other", @"button", @"titles", nil];

So the name of the method is repeated (in order!), and the parameter names are substituted by the actual arguments.

In C#, the corresponding function signature would look like

object InitWithTitleAndMessageAndDelegateAndCancelButtonTitleAndOtherButtonTitles(
        string title,
        string message,
        object delegate,
        string cancelButtonTitle,
        params string[] otherButtonTitles);

and called like

object result = theAllocedAlertView.InitWithBlahBlahBlahAndOtherButtonTitles(
                   "title",
                   "message",
                   someDelegate,
                   "cancel button title",
                   "other", "button", "titles");
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
0

The method selector is:

tableView:cellForRowAtIndexPath:

where each value after the colon is a parameter. The signature is meant to read like an English sentence, i.e. "The TableView's cell for a row at this index".

MarkPowell
  • 16,482
  • 7
  • 61
  • 77
  • That is the method's selector, not signature. The selector is the name of the method. The signature is the name + all parameter information. – bbum Jul 07 '10 at 17:14
0

If this were written in another language it might look like this

// @param (UITableView *) tableView
// @param (NSIndexPath*)indexPath
// @return UITableViewCell
- (UITableViewCell *) someFunctionName(tableView, indexPath) {

}

Thats roughly speaking of course. It would not be written like this in objective-c. However I believe it is possible to write a good chunk of your program in c++

cdnicoll
  • 574
  • 1
  • 6
  • 17