Objective-C categories (category)

Source: Internet
Author: User

Objective-C categories (category)
1. When you want to add methods to a class, you usually expand (extend, that is, inherit) it. However, this is not necessarily a perfect solution, especially if you want to rewrite a class

A feature, but you do not have the original code. Categories allows you to add new features to an existing class, but you do not need to expand it. The ruby language has similar functions.

Example 1:
1, myobjectcatename. h:

# Import <Foundation/nsobject. h> # import "myobject. H "// This header file has defined the myobject class. // This line focuses on the following: myobject is an existing class and () is the name of the category, this category extends the methods add and sub @ interface myobject (myobjectcatename)-(myobject *) Add: (myobject *) m;-(myobject *) Sub: (myobject *) m; @ end

2, myobjectcatename. M:

# Import "myobjectcatename. H "// This line focuses on: myobject is an existing class and () is the name of the category. This category extends the myobject Method Add and sub @ implementation myobject (myobjectcatename) -(myobject *) Add: (myobject *) m {return [[myobject alloc] initwithnumerator: numerator + [M numerator];}-(myobject *) Sub: (myobject *) m {return [[myobject alloc] initwithnumerator: numerator-[M numerator];} @ end

2. (The same class) can have only one category with the same name. Other categories must have different and unique names. categories are very useful when creating private methods. Because objective-C

There is no concept of private/protected/public methods like Java, so you must use categories to implement this function. The practice is to move the private method from your class header (. h) file to the implementation (. m) file.

Example 2:

1, myclass. h

#import <Foundation/NSObject.h>@interface MyClass: NSObject-(void) publicMethod;@end

2, myclass. m

#import <stdio.h>#import "MyClass.h"@implementation MyClass-(void) publicMethod {printf( "public method\n" );}@end// private methods@interface MyClass (Private)-(void) privateMethod;@end@implementation MyClass (Private)-(void) privateMethod {printf( "private method\n" );}@end

3. How to use categories
The use of categories in objective-C involves the following situations:
1) extend existing classes

For example, you can add a new method to the class defined in the cocoa framework. The newly added methods also inherit from the quilt class. during runtime, it is impossible to distinguish whether these methods are newly added or the original class has been defined.

2) alternative solution as a subclass

In addition to specifying a subclass to expand existing classes, you can also add new methods to existing classes directly through category. For example, you can add new categories to nsarray and other cocoa classes to extend existing classes. Like defining subclasses, you do not need to know the source code of the extended class.

3) define a class by using multiple code files
For example, you can group all the methods of a large class and put each group of methods into a category. Each category is defined in a file. This is obvious in the development process.

Provides a simple way to group related methods. Similar methods in different classes can be stored in the same file. If multiple developers need to implement a class at the same time, you can simplify code management by dividing the class method into different categories, you can perform incremental compilation on a very large class to reduce the time required for repeated compilation. You can combine common methods for search and reference, if the same class has different implementations for different applications, you can put different parts into a separate file for code maintenance.

4) informal protocols can be affirmed (Protocols)
Although objective-C can allow the use of category to overwrite class methods, or even methods defined in class interfaces, it is strongly not recommended.

CATEGORY cannot completely replace subclass. It has the following major disadvantages:
When the category overwrites an inherited method, the method in the category can call the inherited method by sending a message to the super class. However, if the method covered by category has already been defined in other category of this class, the previously defined method will not be called by the program, in category, it cannot be determined that it can reliably override a method, which has been defined in other category. This problem is particularly prominent when using the cocoa framework. When you want to override a method defined by a framework, the method has been implemented in other category, so that you cannot determine which definition and implementation will be used first, resulting in great uncertainty.
If you re-override and define some methods, this method will often change throughout the framework. For example, if you add the implements wwillclose: In nsobject,
This will cause all windows to call the new implementation method and change the behavior of all nswindows instances. This will bring a lot of uncertainty and may lead to program crash.

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.