Objective-C in Category

Transfer: in Objective-C in Category

One of the Category Objective-C is also very commonly used functions. category can be added to existing class methods, without the need to add a sub-category, another category so that we do not know the case inside a class that implements the method for the class increases.

If we want to increase the methods of a class framework, category is very effective. New category added by the method becomes part of a class at runtime, adding a new method and existing methods there is no difference in the use, by means of the added category class and other methods will be the same as its subclass inherits. Class defines the interface look like class interface definition, but the difference is listed in parentheses category name, they are located behind the class name. Category must import it extends the class interface file, a standard syntax is as follows:

#import "class name .h"

@interface class name (class name)

@end

And classes, classes have to import it to achieve the interface file, a common naming convention, the base file name in this category is the category of extended class name followed by class name. Therefore, a name for the "class name" + "category name" + ". M" implementation file looks as follows:

#import "category name category name .h"

@implementation class name (class name)

@end

Note : category does not declare a new class instance variables, it contains only methods, then all instance variables in class scope, can be accessed by these class methods. They include even those modified @private variables, you can add all of the instance variables declared in the class as a class more than one category, but the name must be different for each category, and each category must declare and implement a different set of methods .

Remember that when we modify a category by category, when all of the objects in its application of this class are functional, with subclasses are not the same, not increase category member variable, we can also use the category to wrote the original class method exists.

Reproduced in: https: //www.cnblogs.com/hxxy2003/archive/2013/04/16/3023668.html

Guess you like

Origin blog.csdn.net/weixin_34032792/article/details/93892253