iOS categories (category)

Source: Internet
Author: User

iOS category and extensions (Extension) Apple's official documentationcategory is used very frequently in iOS development. Especially in the expansion of the system class, we can not inherit the system class, directly to the system class to add methods, the maximum extent of the objective-c dynamic language characteristics.

#import

@interface NSObject (Category)

-(void) MyMethod;

@end

This is the simplest category to use for the NSObject class and adds a method to NSObject.

Points to note in using Category:

(1) The method of category does not have to be implemented in @implementation, but it can be implemented in other locations, but when the method of category is called, the implementation of the method is not found according to the inheritance tree, and the program crashes.

(2) Category theory cannot add variables, but @dynamic can be used to compensate for this deficiency.

#import

static const void * Externvariablekey =&externvariablekey;

@implementation NSObject (Category)

@dynamic variable;

-(ID) variable

{

Return Objc_getassociatedobject (self, externvariablekey);

}

-(void) SetVariable: (ID) variable

{

Objc_setassociatedobject (self, externvariablekey, variable, objc_association_retain_nonatomic);

}

-----------------------------------------------------------------------------------------

Extension is very much like a category that is not named.

@interface Myclass:nsobject

@property (retain, readonly) float value;

@end

In general, extension are placed above the @implementation in the. m file.

@interface MyClass ()

@property (retain, readwrite) float value;

@end

Points to note when using extension:

(1) The method in extension must be implemented in the @implementation, otherwise the compilation will error.

No matter how perfect a class design is, it is inevitable that there will be no predictable demand, so how do you extend the existing class? Of course, inheritance is a good choice. But Objective-c provides a special way to extend the class, called catagory, to dynamically add new behavior to the already existing class. This will ensure that the original base of the class, the smaller changes can increase the required functionality. When you use the category to extend a class, you do not need to access its source code, and you do not need to create subclasses, so that we can extend the class provided by the system . Category uses a simple way to implement the modularity of the relevant methods of the class, assigning different class methods to different classification files .

Use a simple example to see how the category is used.

Now we have a class called MyClass.

    1. #import
    2. @interface Myclass:nsobject
    3. -(void) myprint;
    4. @end
    1. #import "MyClass.h"
    2. @implementation MyClass
    3. -(void) myprint{
    4. NSLog (@ "myprint called");
    5. }
    6. @end

It has an instance method: Myprint, we can call it later after the extension.

Well, with the above MyClass, we want to add a HelloWorld method without adding a subclass and not modifying the MyClass class. Just add two files Myclass+helloworld.h and myclass+helloworld.m.

Use "()" in the Declaration file and implementation file to enclose the category name. The original class name +category "This is the agreed file naming method.

See how these two files are implemented, create a class by pressing command+n on xcoed, creating a new file, and choosing Objective-c category, so Xcode will automatically help you create a file that is named by convention.


Category on class is MyClass, right oh

This will help you create the two files for Myclass+helloworld.h and MYCLASS+HELLOWORLD.M.

So let's add a HelloWorld method now. Take a look at implementing the following code:

    1. #import "MyClass.h"
    2. @interface MyClass (HelloWorld)
    3. -(void) HelloWorld;
    4. @end
    1. #import "Myclass+helloworld.h"
    2. @implementation MyClass (HelloWorld)
    3. -(void) helloworld{
    4. NSLog (@ "Hello London Olympics!");
    5. }
    6. @end
Called in Main

    1. MyClass *myclass = [[[MyClass alloc]init]autorelease];
    2. [MyClass HelloWorld];
    3. [MyClass Myprint];

Run Print results:

    1. 2012-08-09 11:24:16.697 objectivec[16053:403] Hello London Olympics!
    2. 2012-08-09 11:24:16.699 objectivec[16053:403] Myprint called the

What are the usage scenarios for the category:
1, the class contains a number of method implementations, and these methods require different team members to implement
2. When you are working with classes in the base Class library, you do not want to inherit these classes and only want to add some methods.

Category can achieve the above requirements, of course, there are also the use of category is to pay attention to the problem:
1. Category can access an instance variable of the original class, but cannot add an instance variable, if you want to add a variable, it is implemented by inheriting to create a subclass.
2, category can overload the method of the original class, it is not recommended to do so, this will overwrite the original class method. If you do want to overload it, it is implemented by inheriting the subclass.
3, and the common interface is different, in the category implementation of the file instance method as long as you do not call it you can not implement all the methods of the Declaration.

iOS categories (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.