Classification and classification extension

Source: Internet
Author: User

1.Category Introduction

Category, also known as Categories & categories & classification, is the OC-specific syntax, without modifying the original class based on the addition of new methods, a large class can be multi-person to sub-module development, help team cooperation, or the current class method to do a collation, in order to better update and maintenance

Usage of 2.Category

Naming rules: Class name + extension method, such as adding a run method to Car, then named (Car+run)

Select Objective-c file in new file to select Category

In the. h file, declare the category:

@interface the original class class name (category name)

Adding a variable to a category is not allowed

The new method must be written between @interface and @end

New Method declaration

@end

Implementation methods in. h files or. m files

@implementation the original class class name (category name)

New Method implementation

The implementation of the new method, like the implementation of the regular class, must be written between the @implementation and the @end

New method implementations can call member variables of the original class

The new method has the same name as the original class method, and the new method is called first because the original class method is overwritten

When there are multiple classifications and the names of the methods in the classification are the same, the method called is the last one to participate in the compilation

@end

3.Extendsion Introduction

Extension is a special case of category its name is anonymous (empty), and the newly added method must be implemented

(category does not have this restriction)

Usage of 4.Extendsion

Select Objective-c File Select Extendsion in new file

@interface the original class class name ()

{

Extendsion is a new private member variable that can be added

}

New method declaration (the method declared here is a private method)

@end

The declared method is then implemented in the. m file of the original class.

Classification and classification extension

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.