6

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.

4 Answers 4

28

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanx alot, its realy a useful and detail descriptive post
If you found this helpful. Please mark this as the answer to your question (checkmark to left of this post). And you may want to consider up-voting this post as well (Up arrow above the '0').
2

This will guide you to have understanding about how to declare functions.

An Overview of Objective-C Functions

Comments

2

initWithTitle is a method name in objective-c,

title and rating are two parameter of initWithTitle method of type string and float respectively.

where ratings is just a name or identifier given to second part of parameter.

you can call this method using: [self initWithTitle:str ratings:0.5];

I hope your doubt is clear now :)

Comments

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

in it you know that

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

same way

ratings:(float)rating ratings:is joined argument, type = float and rating is placeholder name in this function.

1 Comment

Thank you @NAZIK for help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.