Categories and extensions of iOS development

Source: Internet
Author: User

Categories and extensions of iOS development
Category:

In ios project development, you can use a category to add a new method to an existing class without creating a subclass. By using categories, we can dynamically add new methods to existing classes and modularize the definition of classes to multiple related files.

Benefits of using categories:

I. Modular Design

Ii. Use category to call private methods

The Interface format is as follows:

@ Interface existing class (category name)

// Method definition

.....

@ End

Note: Only new methods are defined in a category.

The syntax format of category implementation is as follows:

@ Implmentation existing class (category name)

// Method implementation

@ End

Create an old class first

Then a new category, such

Methods are created in the old class and new class respectively, and finally the method for calling the main. m function is as follows:

 

Extension

Extensions are similar to categories. extensions are equivalent to anonymous categories in the following format:

 

@ Interface existing class ()

// Method definition

.....

@ End

The difference between category and extension is:

Classes generally do not add variables again. They only add new methods, but can be extended.

The extension is only implemented in the interface file (. h) but not the method defined in the file (. m ).

 

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.