iOS development OC (ix)--category-categories

Source: Internet
Author: User

Requirement: How to extend some methods to the class without changing the original class model? There are 2 different ways

> Inheritance

> Category (category)

(i) Understand catagory

1.Catagory, can be dynamic for the existing classAdd a new behavior (method), feature extensions, when you extend a class using category, you do not need to create subclasses. Category uses a simple way to implement the modularity of class-related methods, assigning different class methods to different classification files

Category (categories, categories, categories)--(For functional classification, Division of labor, conducive to development, usually named after the function)

2. Example

// Statement @interface     class name (   @end// implementation @implementation     class names (  Category name )@end

3. created by the New File ,-->os X (under Cocoa)-->objective-c category-->

Note:#import "Dog+yy.h" is to say clearly what the classification is called

/*

The function of classification: You can add some methods to the class without changing the contents of the original class.

Use Note:

1. Classification can only increase the method, cannot increase the member variable

2. member variables declared in the original class can be accessed in the classification method implementation

3. Classification can re-implement the methods in the original class, but will overwrite the original method, will cause the original method can not be used

4. Priority of method invocation: Classification (the last category that participates in compilation takes precedence)--and the original class---The parent class

*/

iOS development OC (ix)--category-categories

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.