Keyword category for iOS development

Source: Internet
Author: User

I. When using C ++ for development, we can easily add new methods to existing classes using inheritance combinations to expand existing classes. However, in object-C, it is more convenient to add new methods to existing classes. Therefore, we need to use a special attribute category, however, when we use category again, we should pay attention to the following two points:

1: If you add a new method name to an existing class that is the same as the existing method name, the existing class (equivalent to Rewriting Method) will be overwritten, So that you cannot access the previous method;

For example, we want to add a sort method to nsarray and arrange the following arrays from small to small.

 Nsarray* Arr = [NsarrayArraywithobjects:@ "12",@ "111",
@ "2",@ "67",@ "45",
@ "80",Nil];

@ Interface nsarray (sortsuppot)

-(Nsarray *) sortarraybythismod :( nsarray *) array;

@ End


@ Implementation nsarray (sortsupport)

-(Nsarray *) sortarraybythismod :( nsarray *) array {

// Specific implementation method

}

@ End

2: You can only add methods to existing classes, but cannot add attributes (variables );

However, we can use inheritance to add new property variables to existing classes.

@ Interface nsaddarray: nsarray {

Nsobject * OBJ;

}

-(ID) dosomething :( nsobject *) OBJ;

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.