Objective-C learning note _ class Extension

Source: Internet
Author: User

Objective-C learning note _ class Extension

Definition of a Category and the use of Extension and the use of three protocols and the use of delegate

I. definition and use of Category

Category, Category or Category. The main function is to add methods for classes without source code. The method added with Category will become part of the original class. To expand a class.

Define the Category Process

New? Select the Objective-C Category template and enter the class name and Category name. h? Add parts? Method declaration. m add? Method implementation

Category statement

NSString + SayHi. h file

@interface NSString (SayHi)- (void)hi;@end

Category implementation

NSString + SayHi. m file

# Import "NSString + SayHi. h" @ implementation NSString (SayHi)-(void) hi {NSLog (@ "Is this the hi added for NSString through category? Method ");} @ end

Category and? Class differences

  Category) Subclass (Subclass)
Function You can only add methods to a class. Add methods and variables for Classes
Features The newly added method will become part of the original class and can inherit from the quilt class. The newly added method is available only for child classes and does not exist for the parent class.
Use Use the instance of the original class (-number method) or use the original class (+ number method) to call the Method Subclass can be called,
Ii. definition and use of Extension

Extension. The main function is to manage the "private" method. When designing a class for Object-Oriented Programming, some methods need to be made public (called interfaces), and some methods are only used internally. The Extension function is to help us manage these internal methods ("private" methods ).

Define the Extension process

The syntax format of Extension is similar to Category, which is equivalent to placing the. h file of Category into the. m file of the original class. Extension is for this class and must have the source code class.

Xxx. m file

@ Interface xxx (ExtensionName) // your method list @ end @ implementation xxx // method implementation @ end

Differences between Category and Extension

  Category Extension
Function Add methods for classes without source code Private management methods
Format Define a pair of. h and. m Write the code to the. m file of the original class.
Iii. definition and use of Protocol

Protocol is a common technology used in iOS development. The Protocol is a set of standards (a bunch of method declarations), with only. H files. Like a task list, on? Write a bunch of things to handle. Who should hand over the list. The method defined in the Protocol. That is, who should hand over the list to complete the tasks specified in the list.

There are two types of Protocols: formal and informal.

The method in the Protocol is required by default, that is, @ required. Keyword @ optional modified? Method is optional and can be implemented or not implemented.

Protocol and proxy

Declares that the Protocol sets the agent attribute so that the agent can call the method (notify the agent to execute the method) to sign the Agreement and designate the agent to implement the Protocol method.

Take marriage as an example (female and male)

Declaration agreement (female) sets the agent attribute (female) to allow the agent to call the method (notify the agent to execute the method) (Female) sign the Agreement (male) designated agent (female) to implement the Protocol method (male)

Code Implementation of the wedding example:

The Girl. h file code is as follows:

# Import
  
   
# Pragma mark protocol Step 1/* declaration protocol */@ protocol Marry
   
    
/* There are two methods */@ required/* required */-(void) makeMoney; @ optional/* optional */-(void) washCloth;-(void) cook; @ end @ interface Girl: NSObject # pragma mark protocol Step 2/* Set proxy attributes. Note: assign */@ property (nonatomic, assign) id
    
     
Delegate; @ property (nonatomic, retain) NSString * name; @ property (nonatomic, assign) NSInteger age; # Step 3 of The pragma mark protocol/* notify the agent to call the Method */-(void) marry; @ end
    
   
  

The code for the Girl. m file is as follows:

# Import Girl. h @ implementation Girl # Step 3 of The pragma mark protocol/* notify the agent to call the Method */-(void) marry {NSLog (@ me and % @ get married, % @ is responsible for making money ., self. delegate, self. delegate); [self. delegate makeMoney];} @ end

The Boy. h file code is as follows:

# Import
  
   
# Import Girl. h/* can only be import */# Step 4 of The pragma mark protocol/* sign the Agreement */@ interface Boy: NSObject
   
    
@ Property (nonatomic, retain) NSString * name; @ property (nonatomic, assign) NSInteger age; @ end
   
  

The code for the Boy. m file is as follows:

# Import Boy. h @ implementation Boy # pragma mark protocol step 6/* implementation Protocol Method */-(void) makeMoney {NSLog (@ I am % @, I am responsible for earning money ., self. name) ;}@ end

The main. m file code is as follows:

# Import
  
   
# Import Girl. h # import Boy. hint main (int argc, const char * argv []) {@ autoreleasepool {/* Create Girl and Boy objects */Girl * girl = [[Girl alloc] init]; girl. name = @ LiSi; girl. age = 22; Boy * boy = [[Boy alloc] init]; boy. name = @ WangLaowu; boy. age = 25; # Step 5 of The pragma mark protocol/* specify an agent */girl. delegate = boy; [girl marry];} return 0 ;}
  
Use of delegate

What is the Protocol core? The use case is to implement the delegate design mode. Delegate (agent), generally speaking, is the agent, the main task is to help you complete
Into some tasks. For example, a nanny can think of it as delegate. The main task is to help you take your children, cook, and wash clothes.

Enable? Scenario: What are some tasks? If you want others to implement it, you can specify a proxy to help you. You only need to notify the proxy to do something.

 

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.