Use of iOS category categories

Source: Internet
Author: User

Use of iOS category categories

OBJECTIVE-C provides a very flexible class extension mechanism-category. The category is used to add a method (Methods) to an already existing class. You just need to know the public interface of this class, and you don't have to know the source code of the class. It is important to note that a category cannot add an instance variable (Instance Variables) to a class that already exists.

The basic syntax for a category is as follows:
@interface ClassName (CategoryName)
Method declarations
@end
@interface class name (category name)
Category method declaration
@end

Note the points:

1. The class name of the existing class is after the @interface

2. The name of the category in parentheses (as long as the name is unique, you can add as many categories as possible)

3. Category has no instance variable part

The syntax of a category is very similar to the syntax of a class. The method of the class is the method of the classes. The definition of a category can be placed in a separate file ("category name. h"),  can also be placed in the definition file (. h file) of an existing class. The implementation of a category can be placed in a separate "category name. m" File, or in an implementation file of another class. This is similar to the definition of a class. Because the method of a class is a method of the classes, the methods of the class can freely refer to the instance variables of the classes (whether public or private). The   subclass (subclassing) is another common method for class expansion. The main advantage of a class compared to subclasses is that classes that already exist in the system can use the extended functionality of the category without modification. For example, suppose there is a class A in the system, another class B defines an instance variable of type A, and contains the header file "#import <A.h>" of Class A. Suppose that after a period of time, several new methods need to be extended to class A. If you use subclasses, you need to create a subclass of A-1. If Class B wants to use the new method of class A, make the following modifications: 1) Change the included header file to "#import <A-1.h>"; 2) Change all used Class A objects to objects of class A-1. As you can imagine, there are many classes that require new features of Class A (for example, Class A is the class Uiviewcontroller in iOS), and as the system is upgraded (iOS from 1.0 to 5.0), your program needs to keep making such tedious changes. If you use a category, even if Class A is upgraded, the other classes in the system can call the new method of Class A directly without any modification. The second major advantage of the    category is the realization of localized encapsulation of functionality. A category definition can be placed in the definition file (. h) of an existing class (Class A). This means that the category will be seen externally only if Class A is referenced. If another class (Class B) does not need to use the functionality of Class A (. h files that contain Class A), you will not see a category where dependency Class A exists. This class definition method is widely used in the IOS SDK to encapsulate functionality. For example, the Uiviewcontroller category that is specifically extended for Uinavigationcontroller is defined in  uinavigationcontroller.h:  @interface Uiviewcontroller (Uinavigationcontrolleritem)
@property (Nonatomic,readonly,retain) Uinavigationitem *navigationitem;
@property (Nonatomic,readonly,retain) Uinavigationcontroller *navigationcontroller;
......
@end
If a class does not refer to UINavigationController.h, it will not see both Navigationitem and Navigationcontroller declarations (declared property). Another advantage of the category is its lightness (light-weight). Many times, the extensions required for existing classes are just a few new methods. In this case, the category avoids leaving a lot of very short "micro" subclasses in the system, which makes the program more compact. Induction:

1. Realization Category

Similar to the implementation class, the implementation method can

2. Limitations of categories

1. Categories cannot add new instance variables

2. Naming conflicts, categories have higher precedence if methods and methods in the class already have the same name

3 Role of the category

1. Dispersing the implementation of a class into multiple different files or multiple frameworks

2. Creating a forward reference to a private method

3. Adding an informal agreement to an object

4 using category dispersion implementations

Classes can be used to spread the methods of a class into multiple source files

In particular, categories can access instance variables of their inherited classes

When using a method, it is not important that the object's methods are declared in the interface, declared in the parent class, or declared in a category

Categories can be distributed not only to different source files, but also to cross-frame

5. Use categories to create forward references

Although an undeclared method can be implemented, the compiler warns

A declaration can be provided through a category, and the method of declaration is not necessarily implemented in the implementation of a class, but can also be implemented in the implementation of classes

6. Informal agreements and delegate categories

A delegate (Delegage) is an object, and an object of another class requires that the delegate object perform some of its operations

A delegate object accepts calls from other objects to its specific methods

In fact, a delegate object must implement a method called by another object, similar to an interface

7. Itunesfinder Project

8. Delegation and Category

What is the relationship between a delegate and a category? Another application of delegated emphasis categories: The method that is sent to the delegate object can be declared as a nsobject category

Creating a nsobject category is called "Creating an Informal Protocol"

9. Response Selector

The selector is just a method name, and you can use the @selector () precompiled directive to specify the selector, where the method name is in parentheses, but it is encoded in a special way that the OC runtime uses to quickly execute the query

NSObject provides a respondstoselector method that asks the object to determine whether it implements a particular message

10, other applications of the selector

Selectors can be passed, can be used as method parameters, and can even be stored as instance variables

Use of iOS category categories

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.