There are compile-time polymorphic and run-time polymorphism in C #, Java, and only runtime polymorphism in OC, which is related to its operating mechanism. In OC, the invocation of a method is done through the delivery of the message. In the iOS runtime Dynamic runtime, a http://www.cnblogs.com/5ishare/p/4708647.html is mainly about the next run-time process, which mainly looks at message forwarding (the basis for polymorphism).
I. INTRODUCTION
There are two .h,<objc/runtime.h> and <objc/message.h> in the <objc/objc-runtime.h>, this article mainly understands <objc/message.h>
Two. Message forwarding
In this class there are mainly 3 Chinese law (other kinds I do not know so there are 3 kinds): Objc_msgsend, Objc_msgsendsuper, Method_invoke.
Like Objc_msgsend_stret, Objc_msgsend_fpret function returns struct, floating-point number these need to go through the CPU special processing, so do not be too careful. These are the 3 variants above, and when the structure, floating-point numbers are returned, they are called _stret, _fpret.
Three. Use of methods
There are a few points to note before using:
1. To introduce frames <objc/objc-runtime.h>
2. Direct write run-time code will be the error needs to press set
1). objc_msgsend
In the iOS runtime Dynamic Runtime, a blog also writes that the method call in OC is actually converted to Objc_msgsend for message forwarding.
2). Objc_msgsendsuper
This is to forward the message to the parent class. The first parameter of a method is a struct of type objc_super. The structure consists of two variables:
1.receiver: The actual recipient of the message
2.superClass: Pointer to the parent class of the current class
struct objc_super { // Specifies an instance of a class. __unsafe_unretained ID receiver; /// specifies the particular superclass of the instance to message. __unsafe_unretained Class super_class; };
ID objc_msgsendsuper (struct objc_super *super, SEL op, ...)
3). Method_invoke
There is invoke in C # multi-threading, which is similar to C # for invocation of methods. It is necessary to transfer the structure method. A method in a class can be obtained by runtime.h
Method_invoke (ID
Iv. test Results
A Father class and a son that inherits father are created in the project. The Son class overrides the parent class's-(NSString *) getnamewithfamily: (NSString *) Family method.
#import <Foundation/Foundation.h>@interface father:nsobject-(NSString *) Getnamewithfamily: (NSString *) family; @end
////FATHER.M//RunTime////Created by City--online on 15/11/24.//copyright©2015 year City--online. All rights reserved.//#import "Father.h"@implementationFather-(NSString *) getnamewithfamily: (NSString *) family; { return[NSString stringWithFormat:@"Father%@", family];}@end
// // Son.h// RunTime//// Created by city--online on 15/ 11/24. // copyright©2015 year city--online. All rights reserved. // #import " Father.h " @interface Son:father @end
////son.m//RunTime////Created by City--online on 15/11/24.//copyright©2015 year City--online. All rights reserved.//#import "Son.h"@implementationSon-(nsstring*) getnamewithfamily: (NSString *) family{return[NSString stringWithFormat:@"Son%@", family];}@end
1.objc_msgsend calls son's getnamewithfamily by Objc_msgsend: The method returns as a string, a parameter
Son *son=[[son Alloc]init]; *name= Objc_msgsend (son, @selector (getnamewithfamily:),@ "Tom"); NSLog (@ "%@", name);
-One-up: £ º26.705 runtime[6514:223611 ] Son Tom
2.objc_msgsendsuper calls the parent class through objc_msgsendsuper like [super XXXXX], primarily the first objc_super type of struct.
struct Objc_super Objcsuper; Objcsuper.receiver =father; Objcsuper.super_class =[son superclass]; NSString *supername=objc_msgsendsuper (&objcsuper, @selector (getnamewithfamily:), @ " cui " ); NSLog ( @ " %@ , supername);
-One-up: £ º26.705 runtime[6514:223611 ] Father Cui
3.method_invoke
class ], @selector (getnamewithfamily:)); *invokename= Method_invoke (Son,method,@ "Zhao"); NSLog (@ "%@", Invokename);
-One-up: £ º26.705 runtime[6514:223611 ] Son Zhao
class ], @selector (getnamewithfamily:)); *invokename= Method_invoke (Father,method,@ "Zhao"); NSLog (@ "%@", Invokename);
£ º:28.288 runtime[6546:231306 ] Father Zhao
IOS runtime Dynamic Runtime two