Answer the question
In order to leave comments, you need to log in
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
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
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.
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 ...
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.
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 questionAsk a Question
731 491 924 answers to any question