Objective_C extension mechanism Learning

Source: Internet
Author: User

Objective_CExtensionMechanismLearning is the content to be introduced in this article.Objective_CIt has been one year since the iphone was developed. FirstObjective_CWhen c/c ++ is used, the knowledge learned is based on c/c ++. In factObjective_CThe superset of C was not realized at that time, and its essence was not understood at all. Learn more with the passage of time.

Objective_CIt is more powerful than c/c ++ because it contains some design patterns. I have heard that java includes almost all the design patterns, but I have never used java in depth, and I have used j2s to write a little logic. The two most flexible aspects of Objective_C are: category and associative. I classify them as extensions of Objective_C.Mechanism. Category can extend a class method, and associative can extend the attributes of a class. The functions of these two methods are equivalent to the inheritance in c ++.

Let's take a look at the associative column. The header file is required:

 
 
  1. #import <objc/runtime.h> 
  2.  
  3. static char overviewKey = 'a';    
  4. NSArray *array = [[NSArray alloc] initWithObjects:@ "One", @"Two", @"Three", nil];    
  5. // For the purposes of illustration, use initWithFormat: to ensure      
  6. // we get a deallocatable string      
  7. NSString *overview = [[NSString alloc] initWithFormat:@"%@", @"First three numbers"];    
  8. objc_setAssociatedObject ( array, &overviewKey, overview, OBJC_ASSOCIATION_RETAIN);     
  9. [overview release];    
  10. NSString *associatedObject = (NSString *) objc_getAssociatedObject (array, &overviewKey);    
  11. NSLog(@"associatedObject: %@", associatedObject);    
  12. objc_setAssociatedObject ( array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN );     
  13. [array release];    
  14.  static char overviewKey = 'a';  
  15.  NSArray *array = [[NSArray alloc] initWithObjects:@ "One", @"Two", @"Three", nil];  
  16.  // For the purposes of illustration, use initWithFormat: to ensure   
  17.  // we get a deallocatable string   
  18.  NSString *overview = [[NSString alloc] initWithFormat:@"%@", @"First three numbers"];  
  19.  objc_setAssociatedObject ( array, &overviewKey, overview, OBJC_ASSOCIATION_RETAIN);   
  20.  [overview release];  
  21.  NSString *associatedObject = (NSString *) objc_getAssociatedObject (array, &overviewKey);  
  22.  NSLog(@"associatedObject: %@", associatedObject);  
  23.  objc_setAssociatedObject ( array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN );   
  24.  [array release]; 

Objc_setAssociatedObject adds an attribute to the array. We can obtain this attribute through the key. See the code above: objc_getAssociatedObject. If the second objc_setAssociatedObject is set to nil, this attribute is deleted.

Here is another example: callback.

Summary: DetailsObjective_CExtensionMechanismI hope this article will be helpful to you!

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.