9. Category-OC

Source: Internet
Author: User

The Category is translated as a directory [system class extension]

(1) Implementation of extension methods beyond inheritance

(2) How to Use Category

(3) string flip method Category Extension

(4) private functions


(1) Implementation of extension methods beyond inheritance

Expand some methods in a class

1. You can divide the implementation of a class into several different files.

<1> c/c ++ cannot implement classes. Classes are implemented in several files.

<2> Expand some methods in the existing class.

In the past, we used to implement some methods in the inheritance method. Now we only need to expand some methods in the string class. [Dynamically add some methods in the system class]

<3> companies often ask what is the difference between Category and inheritance?

Category cannot completely replace inheritance, but it can do a lot of things that inheritance cannot do. It is easier to write than inheritance, but it is easier to use.


2. Each Category is part of the class.

3. Different categories of classes can be compiled separately (different developers can be responsible for a Category)

4. If you put a Category class (declaration and implementation) in A. m file, the Category cannot be accessed from outside. In this way, the private function similar to class in c ++ is implemented.

5. Category is actually an extension of the class.


Disadvantages of Category:

Only functions, messages, character segments, and variables can be extended. [Therefore, inheritance cannot be replaced.]

Category naming rules:

General:

Class name to be extended + extension variable. [h/m]

For example:

NSString + ReverseString. h

NSString + ReverseString. m

Unichar = unsigned short two bytes.

"% C" is a big C, which is different from c in speech.

AppendString is added to the string.



Implementation of. m function privatization [only calls to. m function]

@ Interface Foo (Private) // such declaration-(void) test2; @ end @ implementation Foo-(void) test {[self test2];}-(void) test2 {NSLog (@ "test2 is calling");} @ end


(2) How to Use Category

(3) string flip method Category Extension




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.