IOS runtime learning notes and iosruntime learning notes

Source: Internet
Author: User

IOS runtime learning notes and iosruntime learning notes

I,What is Objective-C runtime?

In short, Objective-C runtime is a C library that implements Objective-C. Objects can be represented by struct in C language, while methods can be implemented using C functions. In fact, they almost did the same thing, and added some additional features. After these struct and functions are encapsulated by the runtime function, the Objective-C programmer can create, check, and modify classes, objects, and their methods when running the program.
In addition to encapsulation, the Objective-C runtime Library is also responsible for finding the final Execution Code of the method. When the program executes [object doSomething], the method is not directly found and called. On the contrary, a message will be sent to an object (here, we usually call it the receiver ). The runtime Library gives an opportunity for an object to determine the response based on the message. Alan Kay repeatedly stressed that message-passing is the most important part of Smalltalk (Objective-C developed based on Smalltalk), not an object.

Objective-C is a runtime-oriented language. So the question is, what is a runtime language? A runtime language is a language that determines the internal implementation of a function and other decisions when the application is running. Is Objective-C a runtime language? No. It is a runtime-oriented language, which means that as long as possible, it will take the decision-making time from the compile time and the link delay to the time when the code is actually executed. As mentioned above, Cocoa provides the runtime environment required by iOS applications.

II,Messages in runtime

① Message)
The specific definition of message is hard to say, because there is no real code description. Simply put, message is an abstraction, including the function name + parameter list, which does not actually exist.
② Method)
Method is a real code. For example:-(int) meaning {return 42 ;}
③ Selector (method selector)
Selector exists through the SEL type to describe a specific method or message. In actual programming, you can use selector to retrieve methods and perform other operations.

III,_ Cmd keyword

-(Void) message {self. name = @ "James"; // assign SEL currentSel = _ cmd to the attribute of the current object through the self keyword; // get the SEL NSLog (@ "currentSel is: % s", (char *) currentSel) corresponding to the current function through the _ cmd keyword );}

Print result:

ObjcRunTime[693:403] currentSel is :message  

IV,Class Definition

typedef struct objc_class *Class;typedef struct objc_object {    Class isa;} *id; 

We can see that there are two struct types, one class struct and the other object struct. All Object Structures of the objc_object have an isa pointer, which points to the class to which the object belongs. At runtime, this pointer is used to check whether the object can respond to a selector. We can see an id pointer at the end. This pointer is actually used to represent an ObjC object, a bit similar to the C ++ generic type. After you get an id pointer, you can get the class of this object and check whether it responds to a selector. This is a common call Method for a delegate.

V,IMP (Method Implementations)

typedef id (*IMP)(id self,SEL _cmd,...); 

An IMP is a function pointer generated by the compiler. After you initiate an ObjC message, the code that will be executed by the compiler is specified by this function pointer.

Reference link:

1. Understand Objective-C Runtime

2. iOS development: in-depth exploration of the runtime mechanism 01-classes and objects

3. Basic Features of Objective-C runtime (1)

4. Message in Objective-C runtime (2)

5. iOS development: Objective-C runtime

6. What is the difference between Objective-C's Runtime and Java's Runtime?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.