grenzfrequence
grenzfrequence

Reputation: 291

I don't understand Objective C method declarations

What does the following method declaration describe?

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

What I don't understand is the description of the result of the method:

(UITableViewCell *)tableView:(UITableView *)tableView 

It should return only one result, but apparently it is returning two. How?

Upvotes: 0

Views: 143

Answers (2)

Arslan Ali
Arslan Ali

Reputation: 17812

That is OK if you are beginner. See, you can never eat a pizza in one byte, you break it down into small pieces so that you can eat it easily.

The same fact has been realized by Objective-C.

Consider a simple case:

Say we define a method in regular C that takes 5 parameters, and it adds the first two, the result of first and second argument is multiplied with the third argument, whose result is divided by the 4th argument, and at the end, previous result is added into fifth argument.

The following would be the definition of the function:

float aLittleComplexFunction(int first, int second, int third, int fourth, int fifth) {
    return ((((float)first + second) * third ) / fourth ) + fifth;
}

Now, note that I couldn't come up with a good function name because it was tough. A function name like addTwoNumbersAndThenMultiplyAndThenDivideAndThenAdd would be overkill.

Now, when it comes to Objective-C, it breaks down into small pieces as following:

float addNumber:(int)first into:(int)second multiplyWith:(int)third divideWith:(int)fourth addWith:(int)fifth {
    return ((((float)first + second) * third ) / fourth ) + fifth;    
}

Same is the case with what you have asked. Remember in Objective-C, function name is split, and each part comes with it's relevant parameter, overall making it easier to get to know the actual purpose of the function.

You might feel it difficult to get started with it, but once you do, you'll hate the typical C way.

Upvotes: 0

Sulthan
Sulthan

Reputation: 130162

There is actually only one return value (result).

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

in C++/Java would be something like this

UITableViewCell * getTableViewCellForRowAtIndexPath(UITableView *tableView, NSIndexPath *indexPath)

The whole method name in Obj-C is -tableView:cellForRowAtIndexPath:. Obj-C decided to have method names similar to sentences. The parameters are like words in that sentence. Also note we don't usually use get in the beginning of the method name.

In this case, the confusion is understandable because the method name cannot be read as a sentence.

Upvotes: 3

Related Questions