SNAIL-OC Type of study category

Source: Internet
Author: User

A category is a way to add useful functionality or methods to a class that can be a system class, a custom class

Categories are not the same as subclasses, categories can only add some method property variables or not add

You can extend an existing class without creating a new class

Working with other programmers will work better when doing the project.

The diagram for the new category is placed on the last side

We add a printing method for the system class Nsarray to enable the output of Chinese
The interface is as follows

And then declare a function in Nsarray+mynslog.h

#import <Foundation/Foundation.h> @interface Nsarray (Mynslog)-(NSString *) Printchinese; @end

And then nsarray+mynslog.m.

#import "Nsarray+mynslog.h" @implementation Nsarray (Mynslog)-(NSString *) printchinese{        nsmutablestring * string = [[Nsmutablestring alloc] init];    [string appendstring:@ "{\ n"];    We are a bit system class Nsarray add a function that can print out Chinese (the following self refers to the array we want to output    //That is, to output the Chinese array for    (int i = 0; i < Self.count; i++) {      // The string can be output     in Chinese [string appendformat:@ "\t%@,\n", Self[i]];    }    [String appendformat:@ "}"];        return string;} @end

In main.m, you only need to introduce a category header file, and if it is a custom class, simply enter the header file of the category.

#import <Foundation/Foundation.h> #import "nsarray+mynslog.h" int main (int argc, const char * argv[]) {    @ Autoreleasepool {                Nsarray * array = @[@ "I am", @ "snail", @ "Snail"];        The output of the system is garbled/        *         (         "\u6211\u662f",         "\u8717\u725b",         Snail         ) */        NSLog (@ "%@", Array);                When we use the added function output the Chinese can output to        /*         {         I am,         snail,         Snail,         }         *        /NSLog (@ "%@", [array Printchinese]);    }    return 0;}

Here is a diagram of the new category attached

First of all, the new is suitable for selecting the following file

Next, then write your own defined category name in the first line the second line is which class you want to add the category to

Then save it next, and you'll see that you've created two files of a similar class

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SNAIL-OC Type of study category

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.