Categories, extensions, protocols, and object-c categories in object-c

Source: Internet
Author: User

Categories, extensions, protocols, and object-c categories in object-c

Protocol
The protocol only has method declaration (similar to interfaces in other programming languages). The protocol is equivalent to the keyword @ protocol used by everyone. <The protocol to be followed> NSObject @ end @ protocollamcoProtocol <NSObject>
@ Required // required Method
-(Void) study;

@ Optional // Implementation Method
-(Void) work; @ end student. h file # import <Foundation/Foundation. h> # import "lamcoProtocol. h" @ interfaceStudeny: NSObject <lamcoProtocol, bank>

@ End student. m file # import "Studeny. h"
@ ImplementationStudeny
-(Void) study {
NSLog (@ "% s" ,__ func __);
}
//-(Void) work {
// NSLog (@ "% s" ,__ func _); //} @ end main function: # import <Foundation/Foundation. h>
# Import "Studeny. h" # import "OtherStudent. h" intmain (intargc, constchar * argv []) {
@ Autoreleasepool {
Studeny * stu = [[Studenyalloc] init];

// [Stu study]; // determines whether it complies with the protocol if ([stuconformsToProtocol: @ protocol (lamcoProtocol)]) {
// Determine whether the Protocol has this method
If ([sturespondsToSelector: @ selector (study)]) {
[Stustudy];
// [Stu work];
} Else {
NSLog (@ "cannot find a good job ");
}
} Else {
NSLog (@ "no training ");
}

If ([stuconformsToProtocol: @ protocol (lamcoProtocol)]) {
If ([sturespondsToSelector: @ selector (giveMoney)]) {
NSLog (@ "monthly refund ");
} Else {
NSLog (@ "Become a black user ");
}
} Else {
NSLog (@ "NONE ");
}



}
Return0 ;}
Extend
Creates a private method for the class to implement one to multiple methods. m corresponds to multiple. the H file is a class. m files can have multiple extensions. the H file extension can be used for various operations on member variables, attributes, and methods. The extension file MyClass_add.h
# Import "MyClass. h"

@ InterfaceMyClass ()
-(Void) add; @ end class file: Myclass. h # import <Foundation/Foundation. h>

@ InterfaceMyClass: NSObject
-(Void) select;

@ End Myclass. m # import "MyClass. h"
@ ImplementationMyClass
-(Void) select {
NSLog (@ "% s" ,__ func __);
}
-(Void) add {
NSLog (@ "% s" ,__ func __);
} @ End main function main. m # import <Foundation/Foundation. h> # import "MyClass. h" // import the extended header file # import "MyClass_add.h"

Intmain (intargc, constchar * argv []) {
@ Autoreleasepool {
MyClass * class = [[MyClassalloc] init];
[Classselect];
[Classadd];

}
Return0 ;}
Category)
1. you cannot add attributes to a category. You can only add method 2. if @ property is used in the classification, it can only generate the declaration of getter and setter, but not implement 3. for example, if a method with the same name as this class is written in the category, the method in the category is preferentially called. Therefore, when defining classes in the category, try to avoid duplicate names of the methods in this class. in classification, you can access the attribute example defined in this class: Directory

Main function main. m

# Import <Foundation/Foundation. h>

# Import "NSString + CategoryNSString. h"

 

Intmain (intargc, constchar * argv []) {

@ Autoreleasepool {

NSString * str = @ "abc ";

NSLog (@ "% @", [str Reverser]);

NSLog (@ "% d", [str leng]. intValue );

}

Return0;

} Category file: NSString + CategoryNSString. h

# Import <Foundation/Foundation. h>

@ InterfaceNSString (CategoryNSString)

-(NSString *) Reverser;

-(NSNumber *) leng;

@ End category file: NSString + CategoryNSString. m

# Import "NSString + CategoryNSString. h"

 

@ ImplementationNSString (CategoryNSString)

/**

* String inversion Function

*

* @ Param string refers to the input string

*

* @ Return the reverse string

*/

-(NSString *) Reverser {

NSMutableString * str = [NSMutableStringstring];

For (unsignedlongi = (self. length); I> 0; I --){

[StrappendFormat: @ "% c", [selfcharacterAtIndex: I-1];

}

Returnstr;

}

/**

* Convert String Length int type to NSNumber type

*

* @ Param string refers to the input and output strings.

*

* @ Return NSNumber, the length of the string

*/

-(NSNumber *) leng {

NSNumber * num = [[NSNumberalloc] initWithUnsignedLong: self. length];

Returnnum;

}

@ End class objective optimization: Put all classes into one category. MyClass. h # import <Foundation/Foundation. h>

@ InterfaceMyClass: NSObject
-(Void) Select;
@ End
@ InterfaceMyClass (CategoryUpdate)
-(Void) update;
@ End

@ InterfaceMyClass (CategoryAdd)
-(Void) add;
@ End

@ InterfaceMyClass (CategoryDeleta)
-(Void) delect; @ end MyClass. m file # import "MyClass. h" @ implementationMyClass
-(Void) Select {
NSLog (@ "% s" ,__ func __);
} @ End MyClass + CategoryAdd. m file # import "MyClass. h"

@ ImplementationMyClass (CategoryAdd)
-(Void) add {
NSLog (@ "% s" ,__ func __);

} @ End MyClass + CategoryUpdate. m file # import "MyClass. h"

@ ImplementationMyClass (CategoryUpdate)
-(Void) update {
NSLog (@ "modify ");
} @ End MyClass + CategoryDeleta. m file # import "MyClass. h"

@ ImplementationMyClass (CategoryDeleta)
-(Void) delect {
NSLog (@ "delete ");
} @ End summary: 1. in order to avoid more code and class extension. H files are all placed in the class to be extended, and their corresponding. delete the H file 2. step 2: Set the corresponding category. m file, change all of their # import "MyClass + CategoryDeleta" to the original class, such as: # import "MyClass. h "is fine # import, @ class, # the difference between the three include # import is the keyword of the Objective-C import header file, # include is the keyword used to import the C/C ++ header file. If you use the # import header file, it will be automatically imported only once and will not be imported repeatedly, which is equivalent to # include and # pragma once; @ class indicates the declaration of a class in the compiler. When executed, you can view the class implementation file to solve the mutual inclusion of header files. # import <> is used to include system header files, # import "is used to include user header files. @ Class is the mutual reference between the two classes. @ class is used to tell the compiler that there is such A class. If it is used, there is no problem. @ class can also solve the problem of loop love, such as. b. h, while B. h imported. h. The compilation of each header file requires that the object be compiled successfully first. Using @ class can avoid this situation.

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.