Use of Objective-C syntax Category

Source: Internet
Author: User

No matter how perfect a class design is, there will inevitably be unpredictable demands. How can we expand the existing class? Of course, inheritance is a good choice. However, Objective-C provides a special way to extend the class, called Catagory. It can dynamically add new behaviors for existing classes. This ensures that the required functions can be added with minor changes based on the original class. When you use Category to expand a class, you do not need to access its source code or create a subclass. In this way, you can extend the class provided by the system. Category uses a simple method to modularize the class-related methods and allocate different class methods to different classification files.

A simple example shows how to use Category.

Now we have a class named MyClass.

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

# Import "MyClass. h" @ implementation MyClass-(void) myPrint {NSLog (@ "myPrint called");} @ end

It has an instance method: myPrint. We can call it after the extension.

Now, with the above MyClass, we need to add a HelloWorld method without adding a subclass or modifying the MyClass class. How can we add it? You only need to add two files: MyClass + HelloWorld. h and MyClass + HelloWorld. m.

In the declaration file and implementation file, use "()" to enclose the Category name. The original class name + Category "is the agreed file naming method.

See how these two files are implemented. On Xcoed, press Command + N to create a new file and select the Objective-C category method to create a class. In this way, Xcode will automatically create a file with the specified naming method.

The Category on class is MyClass. The correct one is selected.

In this way, Xcode will help you create the MyClass + HelloWorld. h and MyClass + HelloWorld. m files.

Now we add a HelloWorld method. The code after implementation is as follows:

#import "MyClass.h"@interface MyClass (HelloWorld)-(void)HelloWorld;@end
# Import "MyClass + HelloWorld. h" @ implementation MyClass (HelloWorld)-(void) HelloWorld {NSLog (@ "Hello London Olympics! ");} @ End

Call in main

        MyClass *myclass = [[[MyClass alloc]init]autorelease];        [myclass HelloWorld];        [myclass myPrint];

Run the print result:

11:24:16. 697 objectiveC [16053: 403] Hello, London Olympics! 11:24:16. 699 objectiveC [16053: 403] myPrint called

 

What are the application scenarios of Category:
1. The class contains many methods to implement, and these methods need to be implemented by members of different teams.
2. When you are using the classes in the base class library, you do not want to inherit these classes but just want to add some methods.
 
Category can meet the above requirements. Of course, you need to pay attention to the following issues when using Category:
1. Category can access the instance variables of the original class, but cannot add instance variables. If you want to add variables, You can inherit to create subclass.
2. Category can reload the methods of the original class. It is not recommended to do so. This will overwrite the methods of the original class. If you do need to overload it, you can create a subclass by inheriting it.
3. What is different from a common interface is that the instance method in the Category implementation file does not need to implement all declared methods as long as you do not call it.

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

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.