Category in objective-C)

Source: Internet
Author: User

Category in objective-C)


1. Category concept: dynamically add new behaviors (methods) for existing classes)


2. How to Create a category


(1) Use xcode to generate a category

(2) You can manually generate a category and generate two @ interfaces in the header file.

For example:

In student. in the hfile, @ interface Student: nsobject-(void) test1; @ end is the class of student, and test is the name of the class @ interface student (TEST)-(void) Test2; @ end in student. in the M file, @ implementation student-(void) test1 {nslog (@ "test1 ...");} @ End @ implementation student (TEST)-(void) Test2 {nslog (@ "Test2 ...");} @ End

3 Category Description


(1) () represents a classification

(2) test in () represents the name of the category.


(3) classification can only be extended, and member variables cannot be added.


(4) No subclass is required for class extension using category

CATEGORY uses a simple method to modularize the class-related methods and distributes different class methods to different classification files.


4. Use Cases of category

(1) You may want to add new methods to one or more classes when defining a class (for example, a requirement change ).

(2) A class contains many different types of methods that need to be implemented, and these methods need to be implemented by members of different teams.

(3) when using the basic class library, you may want these classes to implement some of the methods you need, such as nsstring + JSON. h, to expand some JSON parsing methods for the nsstring class.




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.