Objective-C polymorphism: dynamic type identification and dynamic binding and dy

A, Objective-C polymorphism

1 concepts: the same interface, different implementation

From different classes can define that share the same name.

Dynamic type can make the program until it is executed to determine the type of the object

Dynamic binding type can make the program until it is executed to determine the actual method of object invocation

2.Objective-C is different from the traditional program design language, it can be run again with new data types and new module program: dynamic type identification, dynamic binding, dynamic loading

3.id type: generic pointer type, weak type, no compile time type checking

Two, the dynamic type identification

1 any subclass of NSObject will inherit the NSObject isa instance variables, and when a subclass of NSObject instantiate the object, the ISA instance variable is always the first instance variables of objects.

2 types of objects

*Class object and then running has always been there.

*The class object is a data structure, the basic information storage class: class size, class name, class version and message functions and the mapping table

*Save the class object information is determined at compile time, the program is loaded into memory.

*Object representing a class, class class object, class method belongs to the class of objects

*If the receiver of the message is the name of the class, the class name is representative of the class object

*When running, all instances of the class are generated from a class of objects, object to instances of the ISA value change into their own address, each instance of the ISA class objects are pointing to the instance, * know the parent information, can respond to the method from the class object.

*The class object can only use the method, example method cannot

3.SEL type

Objective-C at compile time, according to the method name (including the parameter sequence), generating a used to distinguish this method only a marker (ID), this mark (ID) is SEL type at run time, is to find ways to approach the marked by. As long as the method name (including the same sequence of parameters), then their ID are the same. Through the @select () method to obtain the mark indicator. SEL mydraw =@select(draw),

NSSelectorFromString(NSString*);According to the method name identification method

(NSString*)NSStringFromSelector(SEL);By method of SEL type name

Methods 4 dynamic type identification

-(BOOL)IsKindOfClass:classObj is a classObj class or subclass

-(BOOL)If isMemberOfClass:classObj is an instance of classObj

-(BOOL)Whether this method in the respondsTosSelector:selector class

NSClassFromString(NSString*);The class object is obtained by the string

NSStringFromClass ([Class] class name); the string by name


Class rectClass= [Rectangle class];Get the class object by name

Class aClass =[anObject class];Get the class objects through examples

if([obj1 class]== [obj2 class])Judgment is not the same class instance


5 objects can be divided into ID type and static type

– If not related to polymorphism, as far as possible the use of static type

– Static type can be better at compile time rather than runtime refers to the wrong

– Static type can improve the readability of the program

Three, dynamic binding

1 in the Objective-C, the methods within an object not call the specified by the compiler decide not decided only by the runtime, this is called methodDynamic binding.


2 in the Objective-C object, method is not called, it receives the message, the message expression: [reciver message]; the runtime system first determines the type of the receiver (dynamic type recognition), then according to the method of message in the list class method selection dependent execution, the source code in the message also known as selector(selector)

The 3 message function:

– Firstly, the first parameter of receiver, find the ISA pointer, then the ISA points to a Class object using the second parameter selector search method;

– If not found, the use of the Class object in the new isa pointer to find the parent Class object level.;

– When found, according to the receiver in the self pointer to the current object, the concrete realization method of calling the current object (IMP), then passes the parameters, the method call.

– If the NSObject Class objects have been found, but could not find you transfer method, it will report does not recognize the message error.


The Method structure in Objetive-C 4

struct objc_method{

SEL method_name;//Method name

char *method_types; //Method.

IMP method_imp; //Method.(IMP)

};

typedefobjc_method Method;

5. What is IMP

– IMP is the abbreviation of "implementation", it is the objetive-C method (method) code block address, similar to a function pointer, it can directly access any one method. Avoid sending messages.

6 methods to obtain IMP

– -(IMP)methodForSelector:(SEL)aSelector;

SEL print_sel =NSSelectorFromString(@"print:");//SEL IMP imp=[person methodForSelector:print_sel]; // IMP imp (person, print_sel, @ "*********"); // IMP directly through the method of equivalent call call:[person print_sel:@"*********"];

– The first parameter imp is the object itself (self), the second parameter is the method of marking, third is the method of parameter

Four, the dynamic loading: runtime loading new classes

Create a new class at run time, and only 3 steps:

1, Class pair allocates storage space, the use of objc_allocateClassPairFunction

2, The increased use of need methodclass_addMethodFunction, increase the instance variable class_addIvar

3 , Useobjc_registerClassPairFunction to register the class, so that it can be used by others.

Note: please use these functions.#import <objc/runtime.h>

Started by Donahue at December 08, 2016 - 5:58 PM

The first collection, thank you.

Posted by Natasha at December 19, 2016 - 6:36 PM