52 Essentials of iOS Development summary of the three

Source: Internet
Author: User

11 about the role of the Objc_msgsend

Its prototype is: void Objc_msgsend (id self, SEL cmd,...)

Objc_msgsend_stret if the message to be sent is to be returned to the struct, this function is processed;

Objc_msgsend_fpret If the message returns a floating-point number, this function is processed;

Objc_msgsendsuper If you want to send a message to the superclass, let this function handle it.

The message is composed of the recipient, the selector, and the parameters. Sending a message to an object (invoke a messages) is also equivalent to "call a method" on the object.

All messages given to an object are handled by the dynamic message dispatch system (dispatch systems), which detects the corresponding method and executes its code.

12 Message forwarding mechanism

After the object receives a message that cannot be read, the class method + (BOOL) Resolveinstancemethod is called First: (SEL) Selector

Recipient, Recipient has a second chance to process an unknown selector, call-(ID) Forwardingtargetforselector: (SEL) Selector

Finally, the full message forwarding mechanism is enabled, the Nsinvocation object is created, the full details of the unhandled message are encapsulated, and the call-(void) Forwardinvocation: (nsinvocation *) invocation

If the object cannot respond to a selector, it enters the message forwarding process.

With the dynamic method parsing function at run time, we can add it to a class when we need to use a method.

objects can transfer certain selectors that they cannot interpret to other object processing.

After these two steps, if there is still no way to handle the selection, then start the complete message forwarding mechanism.

13 Debugging "black Box Method" with "Method leveling technology"

At run time, you can add or replace a method implementation of a selection sub-object to a class, calling void Method_exchangeimplementations (method m1,method m2)

Call Method Class_getinstancemethod (class AClass, SEL Aselector) to get methods

Using a different implementation to replace the original method implementation, which is called "Method leveling", developers often use this technique to add new functionality to the original implementation.

In general, it is not advisable to misuse a method when it is only necessary to debug the program at run time.

14 Understanding the purpose of "class object"

The data structure used to describe the Objective-c object is defined in the header file of the run-time library, and the ID type itself is defined here

typedef struct objc_object{

Class Isa;

}*id;

The first member of each object struct is a variable of class class that defines the class to which the object belongs, called the ISA pointer. The class object is also defined in the header file of the run-time library:

typedef struct OBJC_CLASS *class;

struct objc_class{

Class Isa;

Class Super_class;

const char *name;

Long version;

Long info;

Long instance_size;

struct Objc_ivar_list *ivars;

struct Objc_mehtod_list *methodlists;

struct Objc_cache *cache;

struct Objc_protocol_list *protocols;

};

Each instance has a pointer to a class object to indicate its type, and these class objects form the inheritance system of the class.

If the object type cannot be determined at compile time, then the type information query method should be used to detect, "Ismemberofclass", "Iskindofclass".

Try to use Type information query methods to determine object types, rather than directly comparing class objects, because some class objects may implement message forwarding.

15 avoiding namespace collisions with prefixes

Choose a name that is associated with your company, application, or both as a prefix to the class name, and use that prefix in all your code.

If a third-party library is used in the library you are developing, you should prefix the name.

52 Essentials of iOS Development summary of the three

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.