tags:

views:

94

answers:

7

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.

+4  A: 

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
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
@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
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
A: 

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
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
Fixed, thank you.
MarkPowell
+6  A: 

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
A: 

This was my starting point. You can have a look for a quick overview of obj-c syntax.

taskinoor
+1  A: 

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
+3  A: 
(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:.

sdolan
Objective-C does not have named parameters.
bbum
@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.
sdolan
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
A: 

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