Objective-c Keynote (4) Category

Source: Internet
Author: User

OC It provides a different way of--category, which can dynamically add new behaviors to existing classes (methods), which ensures the original design of smaller classes, and then gradually joins the extensions.

You do not need to create a subclass when you are using category expansion classes, and the category uses simple methods. Implement class-Modularization related methods, and class methods are assigned to different classification files.

Let's take a look at the classification using three examples:

Following our previous code, we create a student test class. Create steps such as the following:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgf3yw5nyw5iyw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>



Note that the above two files of Student+test.h and STUDENT+TEST.M are the classification files we created for the student class.

Student+test.h file

#import "Student.h" @interface Student (Test)-(void) Test; @end
STUDENT+TEST.M file

#import "Student+test.h" @implementation Student (Test)-(void) test {    NSLog (@ "called the test method for the test class of Student");} @end
MAIN.M file

#import <Foundation/Foundation.h> #import "Student.h" #import "student+test.h" int main (int argc, const char * argv[ ] {    @autoreleasepool {        Student *stu = [[[Student alloc] initstudent:23] autorelease];        [Stu Test];    }    return 0;}
Execution Result:

2014-11-16 11:32:00.861 memory management [582:33,690] students aged 23 were created

2014-11-16 11:32:00.862 memory management [582:33,690] called the test method of the student test class

2014-11-16 11:32:00.862 memory management [582:33,690] students aged 23 were released.

Apart from this, the classification can actually be written directly into Student.h and STUDENT.M without creating a separate file.

We can also classify system classes (NSString), for example, by adding a method to NSString that handles JSON.

#import <Foundation/Foundation.h> @interface NSString (JSON) + (void) JSON; @end
#import "Nsstring+json.h" @implementation nsstring (JSON) + (void) json{    NSLog (@ "{' nam ': ' codeingsnal ', ' age ', 24");} @end

Usage Scenarios for classification:

1, in some cases of defining the class (such as requirements change). You may need to add a new method to one or more of these classes.

2. A class includes many different kinds of methods that need to be implemented. These methods need to be implemented by members of different teams.

3, when using the class of the base Class library, it is possible to want these classes to implement some of their own methods, such as writing a nsstring+json.h, to nsstring this class is to develop some solution JSON method.


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Objective-c Keynote (4) 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.