Use of the category Objective-c

Source: Internet
Author: User
Category Related
category is OC's unique grammar
 
// classification statement

@interface ClassName (CategoryName)

NewMethod; // Add method to category
        // It is not allowed to add variables in categories
@end
ClassName: Which class needs to be extended
CategoryName: the name of the category
NewMethod: extended method
// Implementation of classification

@implementation ClassName (CategoryName)

NewMethod
...
@end
ClassName: Which class needs to be extended
CategoryName: the name of the category
NewMethod: extended method
note:

1. Classification is used to add methods to the original class. It can only add methods, not attributes (member variables).

@interface Person (NJ)
// {
// double _height;
//}
2. @property in the category, only the declaration of the setter / getter method will be generated, and the implementation and private member variables will not be generated

// @ property (nonatomic, assign) double height;
Verification method: call the property declared property in the main function, and assign the value, and it will be broken
3.The attributes in .h in all classes can be accessed in the classification

4. There is the same method in the original class and in the classification, and the method in the classification will be called, that is to say, the method in the original class will be ignored, so the development proposal is not to write the method with the same name in the original class in the classification.

5. If there is a method with the same name in the original class in multiple categories, then the compiler decides who will execute it when calling this method, and will execute the last category that participated in the compilation. How do you see the classification who compiled first?

Therefore, it is not recommended to implement many methods with the same name as the original class in the classification declaration.

Class Extension
Anonymous classification or anonymous extension: classification without name

The extension category is called Extension, which is a special case of Category.

Main role: extend some private member variables and methods for a class
Written in .m file
English name is Class Extension
Class extension writing format

@interface class name ()
@end
Compared with the classification, there is one less classification name, so some people call it "anonymous classification"

Use of Objective-C Category
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.