📜 ⬆️ ⬇️

Understand Objective C: Calling Methods

When a healthy programmer first sees the calls to methods in Objective C, his eyes fall out.

Let's talk about it.

Theory


Imagine that when you call a method, you give human names to each parameter. Now it is fashionable and usually the hash is passed to this argument:
')
Php
$image -> calculateSize (array (
'image' => $ image ,
'width' => 50 ,
'height' => 50
))

Javascript
image . calculateSize ({
image : image ,
width : 50 ,
height : 50
});

This is very convenient and clear in the calling code; no need to remember the parameters and the order of their transfer; You do not need to produce the same methods for a different set of parameters, and so on.

In Objective-C, the creators decided to fix this rule at the level of the syntax of the language: each parameter has a name. It is indicated both when calling and when defining a method. Thus, the practice is rigidly introduced when a programmer has to think about the essence of things, and not just bludgeon code.

But a large number of methods are called with a single parameter. In this case, obviously, it does not make sense to call it separately, since From the very name of the method, everything is usually clear.

$records -> storeData ( 'sirko.db' );
$users -> findNickname ( 'vasya' );

For this case, there is a simplified syntax in Objective C, which usually introduces newbies into a stupor. The first parameter of the method has no name. So: all the parameters are named, but the first is not. In any of the scripting languages, this could look like, say, something like this:

Php
$this -> calculateSize ( $image , array(
'width' => 50 ,
'height' => 50
));

Javascript
image -> calculateSize ( image , {
width : 50 ,
height : 50
});

I think that this is the main problem in comprehending Objective C. An experienced programmer will intuitively understand the logic of almost any programming language, but without this “secret knowledge” you can hardly understand Objective C at once.

It turned out an interesting side effect. I don’t know if it was specifically conceived or historically developed, but the name of the first parameter was put into the name of the method.

For example: findUserByNickname, locateByCoordinates, storeIntoFilename, loadFromTable, setupById and so on. Such a naming system has fully taken root here, with the result that the Objective-C code is on average quite readable.

Practice


And now - slides.

content = [answersTable findById:42];

Calling a method in Objective-C is written in square brackets. At the beginning is the object variable. Then the name of the method and its parameter separated by a colon. answersTable is an instance of some great object, findById is a method, and 42 is a good number. The result is returned in the content. variable content. The mnemonic rule: brackets are replaced by content calculations. (As in tcl ).

In other languages ​​it might look like this:

$content = $answersTable -> findByid ( 42 );

And here is the very definition of the method in a wonderful object:

- ( AnswerContent * ) findById : ( int ) questionId {
...
[ self doSomething : questionId ] ;
...
}

The definition of the object method begins with a minus. In parentheses are the returned data type, a pointer, as in C: (AnswerContent *) . Simply put, this means that the method will return an object of type AnswerContent . Next comes the name of the method ( findById ) and, after the colon, the variable of the first parameter, also with the obligatory indication of its type, also as in C: (int) questionId .

Inside the method, you can access the questionId. variable questionId.

In other languages ​​it might look like this:

Php
public function findById ( $questionId ) {
...
$this -> db -> get ( $questionId );
...
}

Javascript
Smth . prototype . findById = function( questionId ) {
...
this . db . get ( questionId );
...
}

Now - the last and most difficult step in understanding the issue.

masterpiece = [ gallery findImageByWidth : 400 andHeight : 300 ] ;

This is how the method with two parameters is called. The first (" 400 ") has no name, is indicated immediately after the name of the method. The second has a name: andHeight . Notice how the method and its parameters are elegantly named. After a while you get used to writing and reading like this and saying to yourself: “gallery, pls find image by width and height”.

Parameters can be many:

price = [ trade calculateWithPrice : 25.55 volume : 500 value : 3 ticker : aaplTicker ] ;

Inside the square brackets there is only one method call for one object, you won’t get confused.

In another programming language, this might sound like this:

Php
$price = $trade -> calculate (array(
'price' => 25.55 ,
'volume' => 500 ,
'value' => 3 ,
'ticker' => $aaplTicker
));

Javascript
price = trade . calculate ({
price : 25.55 ,
volume : 500 ,
value : 3 ,
ticker : aaplTicker
});

And here is the definition of this method:

- ( float ) calculateWithPrice : ( float ) price volume : ( int ) volumeAmount
value : ( int ) value ticker : ( TickerClass * ) ticker {
...
}

Likewise, the minus, the type of the returned data, the name of the method, the type and variable of the first parameter are indicated. Then, after the space - the name of the next parameter, its type and variable. And so on.

An important point. Inside the method, parameter names are not used. To illustrate this, I called the second parameter volume, but it fits in the variable volumeAmount. Inside the method you can refer to it:

volumeAmount += 10 ;

But if you want to refer to volume - the compiler will object, such a variable does not exist. But the parameter with the name price is placed in a variable with the same name. It's simple:

price = price * 0.90 ; // a better discount for our customer

Most often, the names of variables and parameters for simplicity and write the same.

And for dessert, the method returns nothing without parameters:

Call:
[ self destroy ] ;

Definition:
- ( void ) destroy {
...
}


findings


This method of calling methods, of course, initially causes rejection. This is normal. Without secret knowledge about the first parameter, it is impossible to read the Objective-C code right away, and this is annoying, even more so for experienced programmers. And the accumulated irritation, in turn, is immediately associated with its source.

But if you go the right way - to understand and accept without a fight - then these and other principles become quite familiar and understandable. Objective C has been developing for years by no means stupid people, and there are no fundamentally wrong things. Objective C is beautiful in its own way.

And the experience of programming in Objective C will bring to your practice, among other things, a new culture of naming things. And from this will win any code in any language.

PS for experienced professionals: this is an article for beginners. Therefore, I have somewhat exaggerated the meaning in order to focus on the main thing. In particular, I did not mention how the methods are actually called, and so on.

Source: https://habr.com/ru/post/103221/


All Articles