B
B
buzzi8882014-02-12 22:38:30
Objective-C
buzzi888, 2014-02-12 22:38:30

OOP in Objective-C is the concept of sending messages instead of calling a method, why is that?

I am learning Objective-C and this concept is not clear, why in other OOP languages ​​an instance method is simply called, and in Objective-C this is called sending a message. What is it connected with?
The book (Aaron Hillegass) says that you can use both ways, ie:
native [OBJ method:args]
is equivalent to OBJ.method(args)
but using messages is better and this is also not clear.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexander S, 2014-02-13
@FirstX

Personally, it seems to me, although I myself have just begun to study C-objective, that this is just a historically developed concept, which is fundamentally no different in practice, except for the form of notation.
In theory, yes, there are differences that when a method is called, we immediately switch to the execution of the code block (method) of this very object, and for example, an attempt to switch to code execution when the object is null will cause a problem.
When sending a message, the behavior is more dynamic, in the sense that the object may not respond to our message. If we send a request for a null pointer, then nothing terrible will happen, we simply will not receive a response to our request and that's it.
And the fact that there is a "standard" form of notation is a legacy from C, because C-Objective is only a superset over C. Plus, the type of notation in messages is more readable in English, the arguments do not just go in a list, but harmoniously flow into the name of the method. Although it takes some time to get used to it.
You can see a little more details here

S
Sergei Borisov, 2014-02-13
@risik

This is due to the fact that the OOP paradigm in ObjC is taken from SmallTalk.
Then (in the early 80s) this approach was stylish, fashionable, youthful. For example, the interaction of windows in Windows, whose origins go back to the same period, is built on exactly the same principle (although here people did without creating their own programming language).
C++, although it was created around the same time, was built on slightly different principles. And some "other" OO programming languages ​​(Java and C#) were based on the OOP paradigm in C++, not in SmallTalk. Although some concepts later migrated to these OO languages.

I
IURIA, 2014-02-13
@GxocT

In short, when sending a message, the C function with the passed parameters is called.
Read more here:
blog.zhengdong.me/2013/07/18/a-look-under-the-hood...
And here, page 37 onwards:
www.defcon.org/images/defcon-16/dc16-presentations ...

S
s0L, 2014-02-13
@s0L

The book (Aaron Hillegass) says that you can use both ways, ie:
native [OBJ method:args]
is equivalent to OBJ.method(args)
but using messages is better and this is also not clear.

The square brackets are a method call/message dispatch.
Through a dot - this is an appeal to property. A property must have a getter and a setter, and the name of the getter method is the same as the name of the property by default, so this construction works.
Ideologically, of course, it is better not to call methods through obj.method, so as not to introduce confusion. Methods should be called [obj method] and property should be called obj.propertyName

A
Alexey Storozhev, 2014-02-15
@storoj

Because there are no compiled "classes" as such, there is only a list of methods (in text form) and their correspondence to functions. Methods in objc are normal C functions (of type IMP) with additional self and _cmd parameters.
Classes are just structures with enumeration of properties, methods, ivars, superclass and everything else, and the method is called through the search for a function corresponding to the selector (message)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question