turn from: http://blog.jobbole.com/45963/
In the past few years, a lot of objective-c developers have emerged. Some are turned from dynamic languages, such as Ruby or Python, and some are transferred from a strongly typed language, such as Java or C #, and of course there are direct objective-c as an introductory language. That is to say, a large percentage of developers are not using objective-c for too long. When you touch a new language, you will pay more attention to the basics, such as grammar and features. But there are usually some more advanced, less-known and powerful features waiting for you to explore.
This article is mainly to appreciate the Objective-c runtime (runtime), while explaining what makes objective-c so dynamic, and then feel the technical details of these dynamics. Hopefully this will give you a better understanding of how objective-c and cocoa are running. The Runtime
Objective-c is a simple language, 95% is C. Just add some keywords and syntax to the language level. What really makes Objective-c so powerful is its runtime. It's small but very powerful. The core of it is message distribution. Messages
If you are moving from a dynamic language such as Ruby or Python, you may know what a message is, and you can skip to the next section directly. Those transferred from other languages, continue to look.
Executes a method, in some languages, where the compiler performs some additional optimizations and error checking because the invocation relationship is straightforward and obvious. But for news distribution, it's not that obvious. You do not have to know whether an object can handle the message before you send it. You send the message to it, it may be processed, or it may be transferred to other object for processing. A message does not have to correspond to a method, an object may implement a method to handle multiple messages.
In Objective-c, messages are implemented through the runtime method of Objc_msgsend () and similar methods. This method requires a target,selector and some parameters. In theory, the compiler simply turns message distribution into Objc_msgsend to execute. For example, the following two lines of code are equivalent.
1 2 |
[Array Insertobject:foo atindex:5]; Objc_msgsend (Array, @ selector (insertobject:atindex:), Foo, 5); |
Objects, Classes, metaclasses
In most object-oriented languages, there are classes and objects concepts. Objects is generated by classes. But in objective-c, classes itself is also objects (which is similar to Python) and can also process messages, which is why there are class methods and instance methods. Specifically, the object in Objective-c is a struct (struct), and the first member is ISA, which points to its own class. This is defined in the objc/objc.h.