Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Syntax in Objective-C

People also ask

How do you write a method in Objective-C?

An Objective-C method declaration includes the parameters as part of its name, using colons, like this: - (void)someMethodWithValue:(SomeType)value; As with the return type, the parameter type is specified in parentheses, just like a standard C type-cast.

How the function can be defined in Objective-C?

In Objective-C, all the programs contain a C function that is main() . We can also divide the code into different functions. Each performs a specific task. In a function declaration, the name of the function, its return type, and parameters are specified.

How do you declare an object in Objective-C?

Creating a new object in Objective-C is usually a two-step process. First, memory has to be allocated for the object, then the object is initialized with proper values. The first step is accomplished through the alloc class method, inherited from NSObject and rarely, if ever, overridden.

What is @property in Objective-C?

The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.


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


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)

Adding to the previous answers, I'd just like to say that Objective-C methods (or messages if you prefer) have external and internal parameter names.

So in this case:

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

numberOfRowsInComponent is the external name, the one that you would use when calling this method from the outside.

And component is the internal name of the parameter, the one you use to refer to the parameter from inside of the method.

Hope that clears it up a bit.


It seems to me that Objective-C method signatures are more like sentences. Each parameter deserves a part in method's name. For instance, in C we could have a method (setPersonData) for setting some information about person:

void setPersonData( char* name, int age, float height ) {

and in Objective-C the method would be more descriptive (setPersonName:andAge:andHeight:), like

- (void) setPersonName: (char *)name andAge:(int)age andHeight:(float)height {