views:

5180

answers:

4

Can someone explain this method declaration syntax for me? In this function, the number of rows of a UIPickerView (slot machine UI on the iPhone) is being returned. From my understanding, the Method is called 'pickerView', and returns an NSInteger.

It passes in a pointer to the UIPickerview called 'pickerView' ... first, why is the method called the same name as the parameter?

Next there is NSInteger parameter called component that tells us which component we are counting the rows for. The logic to decide which is in the body of the method.

What is 'numberOfRowsInComponent? It seems to describe the value we are returning, but it is in the middle of the parameters.

- (NSInteger) pickerView:(UIPickerView *)pickerView 
 numberOfRowsInComponent:(NSInteger)component
{
    if (component == kStateComponent)
     return [self.states count];

    return[self.zips count];
}
+13  A: 

In Objective-C, the name of a method is composed of all of the portions of the declaration that are not arguments and types. This method's name would therefore be:

pickerView:numberOfRowsInComponent:

The method would be equivalent to a C-style function that looked as follows:

edit: (with thanks to Jarret Hardie):

NSInteger pickerViewNumberOfRowsInComponent(UIPickerView * pickerView, NSInteger component)
e.James
+1 - nice short description. Sometimes I find the lightbulb goes on for people if you write the C-style equivalent as "NSInteger pickerViewNumberOfRowsInCompoent(UIPickerView *pickerView, NSInteger component)
Jarret Hardie
Thanks, I like that method name better. :)
e.James
+37  A: 

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) pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component.

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

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

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

  • numberOfRowsInComponent:
    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 foo and bar, I would type:
    [myObject pickerView:foo numberOfRowsInComponent:bar];
    as opposed to C++ style:
    myObject.pickerView(foo, bar);.

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

Hopefully this is helpful!

Zach Gardner
+1 for a great answer. I would suggest changing "factory" to "class" in your first point, since the "+" officially indicates a class method. It just so happens that many "+" methods are factory methods, but that isn't the proper definition.
e.James
Will do, thanks for the correction.
Zach Gardner
Thanks! One additional question:If you were to message an object myObject with the variables foo and bar like you showed:[myObject pickerView:foo numberOfRowsInComponent:bar];Does pickerView refer to the method name or parameter?
Craig
Neither. It is good style for it to refer to both the method name AND parameter, but the full method name is actually pickerView:numberOfRowsInComponent:. If you were to try to invoke pickerView:, you would recieve a runtime error, as the method would not exist.
Zach Gardner
Let me clarify on that: the pickerView: portion by itself SHOULD refer to the following parameter, to follow good coding style. However, it is only 1/2 of the method name. A better example may be [myObject setX:foo Y:bar], where the method is setX:Y:.
Zach Gardner
Wonderful, awesome answer. This is what make StackOverflow great. Thank you!
Frank V
+2  A: 
Karolis
A: 

Hey, I'm a total newbie to objective-c, but isn't there a major inconsistency here?

I mean the method takes 2 arguments, but one has an "external name" and the other doesn't. What gives?

Look - (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

Here we have 2 parameters one is a UIPickerView, the other an NSInteger. The NSInteger (because it comes second???) has its own external name "numberOfRowsInComponent", but the first one doesn't right?

Am I missing something here? Shouldn't the change Objective-C so that the method parameters are treated consistantly?

Paul G.