- Definition and use of a category
- Definition and use of two extension
- Definition and use of three protocol
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
- New parts
- Select objective-c Category Template
- Fill in the class name and category name
- Declaration of the. H-piece add-on method
- . 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
- Claim Agreement
- Set agent Properties
- Have the delegate invoke the method (notifies the delegate to execute the method)
- Sign an agreement
- Designated agent
- Implementing Protocol Methods
Take marriage as an example (the woman and the husband)
- Statement Agreement (woman)
- Set surrogate properties (woman)
- Have the delegate invoke the method (notifies the delegate to execute the method) (the woman)
- Sign an agreement (the male)
- Designated agent (woman)
- 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