Objective-C polymorphism: Dynamic Type Recognition + dynamic binding + Dynamic Loading

Source: Internet
Author: User

 1. Objective-C Polymorphism

1. Concept: Different implementations of the same interface

Different classes can define methods that share the same name.

The dynamic type allows the program to determine the object type until execution.

Dynamic Type Binding allows the program to determine the actual method to call the object until execution.

2. Objective-C is different from the traditional programming language. It can add new data types and new program modules at runtime: dynamic type identification, dynamic binding, and dynamic loading.

3. ID type: Generic pointer type, weak type, no type check during compilation

Ii. Dynamic Type Recognition

1. Any nsobject subclass inherits the ISA instance variable of nsobject. When the nsobject subclass instantiates an object, the ISA instance variable is always the first instance variable of the object.

2. Class Object

* Class objects always exist when they are running again.

* A class object is a data structure with the basic information of the Storage Class: class size, class name, class version, and ing table between messages and functions.

* The information saved by the class object is determined during program compilation and loaded into the memory when the program starts.

* Class objects represent classes, and class objects represent class objects. Class Methods belong to class objects.

* If the message receiver is a class name, the class name represents the class object.

* During runtime, all class instances are generated by class objects. class objects modify the ISA value of the instance to their own address, the ISA of each instance points to the class object of this instance. * The class object can be used to know the parent class information and the methods that can be responded.

* Class objects can only use class methods, but cannot use instance methods.

3. Sel type

During compilation, objective-C will generate a unique identifier (ID) used to distinguish the method based on the method name (including the parameter sequence). This identifier (ID) it is of the SEL type, and the method is searched through method identification during running. As long as the method names (including parameter sequences) are the same, their IDs are the same. You can use the @ select () indicator to obtain the method identifier. Sel mydraw = @ select (draw );

Nsselectorfromstring (nsstring *); obtain the method id based on the method name

(Nsstring *) nsstringfromselector (SEL); obtain the method name of the SEL type.

4. Common Methods for dynamic Type Recognition

-(Bool) iskindofclass: whether classobj is a classobj class or its subclass

-(Bool) ismemberofclass: whether classobj is an instance of classobj

-(Bool) respondstosselector: Specifies whether this method exists in the selector class.

Nsclassfromstring (nsstring *); Class Object obtained by the string

Nsstringfromclass ([class name class]); obtain the string by Class Name

 

Class rectclass = [rectangle class]; obtain class objects by Class Name

Class Aclass = [anobject class]; obtain class objects through instances

If ([obj1 class] = [obj2 class]) judge whether the instance is of the same class

 

5. objects can be divided into ID and static types.

-If polymorphism is not involved, try to use static types.

-The static type can indicate errors in the compilation stage rather than the running stage.

-Static types can improve program readability

3. dynamic binding

1. In objective-C, whether an object calls a specified method is not determined by the compiler but by the runtime. This is called a method.Dynamic binding.

2. in objective-C, an object receives messages instead of calling methods. The message expression is [reciver message]. during runtime, the system first determines the type of the receiver (Dynamic Type Recognition ), then select the dependent method execution in the method list of the Class Based on the message name. The message in the source code is also called the selector)

3. Functions of message functions:

-First, find its ISA pointer through the aggreger of the first parameter, and then use the selector of the second parameter to find the method in the Class Object pointed to by ISA;

-If not found, use the new ISA pointer in the current class object to search for the Class Object of the parent class at the upper level;

-After finding a method, find the current object based on the Self pointer in the aggreger, call the specific implementation method (IMP) of the current object, pass the parameter, and call the implementation method.

-If the class object of nsobject is found and the method you call is not found, an error cannot be identified when the message is sent.

 

4. Method structure in Objetive-C

Struct objc_method {

Sel method_name; // method name

Char * method_types; // method address

IMP method_imp; // method address (IMP)

};

Typedefobjc_method method;

5.What is imp?

-IMP is the abbreviation of implementation. It is the address of the code block implemented by the Objetive-C method. Similar to the function pointer, it can directly access any method. Message sending is free of charge.

6. Obtain the method's imp

-(IMP) methodforselector :( SEL) aselector;

Sel print_sel = nsselectorfromstring (@ "print:"); // obtain sel imp = [person methodforselector: print_sel]; // obtain imp IMP (person, print_sel, @ "**********"); // use IMP to directly call the method equivalent call: [person print_sel: @ "**********"];

-The first parameter of IMP is the object self, the second parameter is the method identifier, and the third parameter is the method parameter.

4. Dynamic Loading: Load new classes at runtime

To create a new class at runtime, only three steps are required:

1. Allocate storage space for class pair and useObjc_allocateclasspairFunction

2. Add required methodsClass_addmethodFunction to add the instance variable to use class_addivar

3. UseObjc_registerclasspairThe function registers this class so that it can be used by others.

Note: To use these functions, refer to # import <objc/runtime. h>

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.