Objective-c the extension of the Learning Notes _ class

Source: Internet
Author: User





    • Definition and use of a category
    • Definition and use of two extension
    • Definition and use of three protocol
      • Use of delegate




I. Definition and use of category


Category, category, or category. The primary role is to add methods to classes that do not have source code. Methods added by category become part of the original class. The ability to extend a class is thus achieved.



Defining the category Process


    1. New parts
    2. Select objective-c Category Template
    3. Fill in the class name and category name
    4. Declaration of the. H-piece add-on method
    5. . m Add method to achieve


Declaration of category



nsstring+sayhi.h file


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


The implementation of category



NSSTRING+SAYHI.M file


 
 
#import “NSString + SayHi.h”
@implementation NSString (SayHi)
-(void) hi
{
   NSLog (@ ”This is the hi? Method added to NSString through category”);
}
@end


The difference between category and? class


category (category) Subclass (subclass)
Function You can only add methods to classes You can add a method to a class or add a variable
Characteristics The newly added method becomes part of the original class and can be inherited by the quilt class The newly added method only has subclasses, and the parent class has no
Use Use an instance of the original class (-number method) or use the original class (+ sign method) to invoke the method Subclass to be called,
Second, the definition and use of extension


Extension, extended. The primary function is to manage the "private" method of the class. Object-oriented programming when designing a class, some methods need to be exposed externally (what we call an interface), and some methods are only used internally. Extension's function is to help us manage these internal methods of use (the "private" method).



Defining the extension process



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



XXX.M file


 
@interface xxx (ExtensionName)
   // your method list
@end

@implementation xxx
   // Method implementation
@end


The difference between category and extension


Category Extension
Role To add a method to a class that does not have source code Private methods for managing classes
Format Define a pair of. h and. m Write the code in the. m file of the original class
Iii. definition and use of protocol


Protocol, protocol, is a common technique used in iOS development. The protocol is a set of criteria (a declaration of a bunch of methods), only the. h file. Like a list of tasks, on? Write a bunch of things to deal with. The list to whom, who will go to complete the task set out on the list. The object that accepts the agreement implements the method defined in the protocol. That is: the list to whom, who will go to complete the tasks set out on the list.



There are two types of agreements: formal agreements and informal agreements.



The methods in the protocol must be implemented by default, that is, @required. The keyword @optional modified method is optional and can be implemented or not.



Protocols and proxies 6 steps


    1. Claim Agreement
    2. Set agent Properties
    3. Have the delegate invoke the method (notifies the delegate to execute the method)
    4. Sign an agreement
    5. Designated agent
    6. Implementing Protocol Methods


Take marriage as an example (the woman and the husband)


    1. Statement Agreement (woman)
    2. Set surrogate properties (woman)
    3. Have the delegate invoke the method (notifies the delegate to execute the method) (the woman)
    4. Sign an agreement (the male)
    5. Designated agent (woman)
    6. Implement protocol Method (man)


The code implementation of the marriage example:



The Girl.h file code is as follows:


#import <Foundation / Foundation.h>

#pragma mark Protocol first step
/ * Declare agreement * /
@protocol Marry <NSObject>

/ * There are two methods * /
@required / * required * /
-(void) makeMoney;

@optional / * Optional * /
-(void) washCloth;
-(void) cook;

@end

@interface Girl: NSObject
#pragma mark protocol step two
/ * Set agent properties, note: assign * /
@property (nonatomic, assign) id <Marry> delegate;


@property (nonatomic, retain) NSString * name;
@property (nonatomic, assign) NSInteger age;

#pragma mark protocol step three
/ * Notify agent to call method * /
-(void) marry;

@end 


The girl.m file code is as follows:


#import "Girl.h"

@implementation Girl

#pragma mark protocol step three
/ * Notify agent to call method * /
-(void) marry
{
     NSLog (@ "I married% @,% @ is responsible for making money.", Self.delegate, self.delegate);
     [self.delegate makeMoney];
}

@end 


The Boy.h file code is as follows:


#import <Foundation / Foundation.h>
#import "Girl.h" / * can only be imported * /

#pragma mark Protocol Step 4
/* sign the agreement */
@interface Boy: NSObject <Marry>

@property (nonatomic, retain) NSString * name;
@property (nonatomic, assign) NSInteger age;

@end 


The boy.m file code is as follows:


#import "Boy.h"

@implementation Boy

#pragma mark Protocol Step 6
/ * Implement protocol method * /
-(void) makeMoney
{
     NSLog (@ "I am% @, I am responsible for making money.", Self.name);
}

@end 


The main.m file code is as follows:


#import <Foundation / Foundation.h>
#import "Girl.h"
#import "Boy.h"
int 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;
         #pragma mark Protocol Step 5
         / * Designated Agent * /
         girl.delegate = boy;
         [girl marry];
         }
        return 0;
     } 
Use of delegate


Protocol's nuclear? The usage scenario is to implement the delegate design pattern. Delegate (agent), popular speaking is the agent, the main task is to help you finish
into some tasks. For example: Nanny can be considered as delegate, the main task is to help you bring children, cooking, washing clothes and so on.



To make a scene: Any task? oneself do not realize, want to let others to achieve, you can designate an agent, let the agent help you to do. You just need to notify the agent to do something.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Objective-c the extension of the Learning Notes _ class


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.