Like Java, Object-c also provides a mechanism for reflection.
Each class has a class, and there are three ways to get the class.
Insert Code here ... Gets the class class clazz = Nsclassfromstring (@ "NSString") by string; NSLog (@ "Clazz type is%@", clazz); Create object ID directly with calzz str = [[Clazz alloc] init]; object to get Class NSLog (@ "%@", [str class]); Class NSLog (@ "%d", Clazz==nsstring.class) is obtained through the classes. NSLog (@ "Hello, world!");
/**
2015-04-28 17:03:18.907 Reflection [1322:303] Clazz type is NSString
2015-04-28 17:03:19.279 Reflex [1322:303] __nscfconstantstring
2015-04-28 17:03:19.280 Reflex [1322:303] 1
2015-04-28 17:03:19.281 Reflex [1322:303] Hello, world!
Program ended with exit code:0
The result of the print is this, _nsstring is just the front end of the class cluster.
**/
For the first and second methods, the class object is obtained according to the classes, and the first code is more secure, and the program compiles the stage to detect the existence of the class that needs to be accessed. Higher performance because there is no need to call methods.
The method for dynamic invocation is as follows. Directly on the code
#import <Foundation/Foundation.h> @interface Jqcar:nsobject@end
#import "JQCar.h" #import <objc/message.h> @implementation jqcar-(void) Move: (nsnumber*) count{ int num = [Count Intvalue]; for (int i=0; i<num; i++) { NSLog (@ "%@", [NSString stringwithformat:@ "car is walking on the road%d", I]);} } -(double) Addspeed: (double) factor{ //Dynamic Call Move Method//Use Performselector to dynamically call the Move method [self performselector:@ Selector (move:) Withobject:[nsnumber numberwithint:2]; [Self performselector:nsselectorfromstring (@ "move:") Withobject:[nsnumber Numberwithint:2]]; Use the Objc_msgsend () function to dynamically invoke objc_msgsend (self, @selector (move:), [NSNumber numberwithint:2]); Objc_msgsend (Self, nsselectorfromstring (@ "move:"), [NSNumber Numberwithint:3]); NSLog (@ "Accelerating%g", factor); return 100*factor;} @end
Using objc_msgsend may be an error
//objc_msgsend () error too many arguments to function call, expected 0,have3
Workaround
Build setting--> Apple LLVM 6.0-preprocessing--> Enable Strict Checking of Objc_msgsend Calls change to NO
#import <Foundation/Foundation.h> #import <objc/message.h> #import "JQCar.h" int main (int argc, const char * Argv[] {@autoreleasepool {//Insert code here ...//get Jqcar class Clazz = nsclassfromstring (@ "J Qcar "); Dynamically create car id car = [[Clazz alloc] init]; Use the Performselector method to dynamically invoke the [car performselector: @selector (addspeed:) Withobject:[nsnumber Numberwithlong:2]]; Use Objec_send to dynamically call Objc_send (car, @selector (addspeed:), 3.4); Defines a function pointer variable double (*addspeed) (ID, sel,double); Get the Addspeed method of the car object, and then assign the Addspeed method to the addspeed pointer variable addspeed = (double (*) (id,sel,double)) [Car Methodforselector:nssel Ectorfromstring (@ "addspeed:")]; Call Addspeed Double speed = addspeed (car, @selector (addspeed:), 3.4); NSLog (@ "acceleration is%g", speed); NSLog (@ "Hello, world!");} return 0;}
iOS development reflection and dynamic invocation summary