IOS development reflection and dynamic call Summary

Source: Internet
Author: User

IOS development reflection and dynamic call Summary
Like java, Object-C also provides a reflection mechanism.

Each Class has a Class. There are three methods to obtain the Class.

 

// Insert code here... // obtain class Class class clazz = NSClassFromString (@ "NSString") through a string; NSLog (@ "the type of clazz is % @", clazz ); // directly use calzz to create the Object id str = [[clazz alloc] init]; // obtain the class NSLog through the object (@ "% @", [str class]); // obtain the class NSLog (@ "% d", clazz = NSString. class); NSLog (@ "Hello, World! ");
/**
17:03:18. 907 reflection [1322: 303] the type of clazz is NSString
17:03:19. 279 reflection [1322: 303] _ NSCFConstantString
17:03:19. 280 reflection [1322: 303] 1
17:03:19. 281 reflection [1322: 303] Hello, World!
Program ended with exit code: 0
The printed result is as follows. _ NSString is only the front end of the class cluster.
**/

 

Both the first and second methods obtain the Class object based on the Class. In comparison, the first method is more secure and the program can detect whether the Class to be accessed exists in the compilation phase. Higher performance, because you do not need to call methods.

The dynamic call method is as follows. Directly Add code

 

#import 
 
  @interface JQCar : NSObject@end
 

# Import "JQCar. h" # import
 
  
@ Implementation JQCar-(void) move :( NSNumber *) count {int num = [count intValue]; for (int I = 0; I
  
   
Objc_msgSend may report an error // objc_msgSend () Too primary arguments to function call, expected 0, have3
   

 

Solution: // Build Setting --> Apple LLVM 6.0-Preprocessing --> Enable Strict Checking of objc_msgSend CILS to NO

 

# Import
    
     
# Import
     
      
# Import "JQCar. h "int main (int argc, const char * argv []) {@ autoreleasepool {// insert code here... // obtain JQCar Class clazz = NSClassFromString (@ "JQCar"); // dynamically create a car id car = [[clazz alloc] init]; // use the performSelector method to dynamically call [car performSelector: @ selector (addSpeed :) withObject: [NSNumber numberWithLong: 2]; // use Objec_send to dynamically call objc_send (car, @ selector (addSpeed :), 3.4); // defines the 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: NSSelectorFromString (@ "addSpeed:")]; // call addSpeed double speed = addSpeed (car, @ selector (addSpeed :), 3.4 ); NSLog (@ "the accelerated speed is % g", speed); NSLog (@ "Hello, World! ");} Return 0 ;}
     
    

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.