Ahsan Ahsan - 6 months ago 12
iOS Question

how to declare function with multiple parameters in iOS

Can anyone explain the following code?

- (id)initWithTitle:(NSString*)title ratings:(float)rating;

(NSString*)title = first parameter , type =string, name = title


rating:(float)rating = ? ? ?
What is the meaning of this ?

Float is a type and what is rating in start if the second rating is the name of parameter.

Answer

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, -(id)initWithTitle:(NSString*)title rating:(float)rating;

- (id)

This first portion indicates that this is an Objective C instance method that returns an id 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.

initWithTitle:

This portion is a part of the message name. The full message name in this case is initWithTitle:rating:. The Objective-C runtime takes this method information and sends it to the indicated receiver. In pure C, this would look like id initWithTitle(NSString* title, float rating). However, since this is Objective-C, additional information is packed into the message name.

(NSString*)title

This portion is part of the input. The input here is of type NSString* and has a local variable name of title.

rating:

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 initWithTitle:foo rating:bar]; as opposed to C++ style: myObject.initWithTitle(foo, bar);.

(float)rating

This is the last portion of the input. the input here is of type float and has a local variable name of rating.

Hopefully this is helpful!

Comments