Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to declare function with multiple parameters in iOS

Tags:

function

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.

like image 701
Ahsan Avatar asked Aug 30 '12 04:08

Ahsan


People also ask

Can function have multiple parameters?

Luckily, you can write functions that take in more than one parameter by defining as many parameters as needed, for example: def function_name(data_1, data_2):

How do you pass multiple parameters?

The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

How many parameters can I add to a function?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

How do you pass a function as a parameter in Swift?

To pass function as parameter to another function in Swift, declare the parameter to receive a function with specific parameters and return type. The syntax to declare the parameter that can accept a function is same as that of declaring a variable to store a function.


1 Answers

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!

like image 185
Manish Agrawal Avatar answered Oct 12 '22 19:10

Manish Agrawal