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.
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):
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 "&".
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With