Ios development-Category, ioscategory

Source: Internet
Author: User

Ios development-Category, ioscategory

Category adds a method to an existing class without changing the existing class to extend the function of the class.

There are multiple ways to expand class functions. For example, we can extend the functions through inheritance. However, in terms of Category and inheritance, we usually give priority to Category.

Using Category to expand a class has several advantages.

1. You do not need to change the existing class files. You can also directly extend the functions of the classes in the framework. In this case, you do not need to change the data type pointed to by the object.

2. We can directly add Category to an existing class. In this case, we can differentiate functions in the class to group functions. If you look at some class files in the framework, you will find that most of them are applicable to Category for feature differentiation.

3. A lot of third-party software uses Category for custom transformation. For example, SDWebimage is a function extension through Category.

4. Category can be used to implement functions jointly developed by multiple people, as long as the functions are well differentiated,

Multiple users can develop functions for the same class at the same time to improve the development efficiency.

Let's take a look at its usage:

1. When creating a new Category file, you can select

2. File: Enter the Category name.

File Type: Select Category

Class: select the Class to be extended

3. After the class name is created, the class name will become the current one. We can see that we have implemented a Function Extension to NSString.

4. Add the method to be extended to the header file. Now we are adding an NSString and reversing the method.

5. implement our expansion method in our implementation file so that we can directly reverse our NSString object.

6. Next, let's take a look at how to use it. When we use it, we import our header file. The object originally directed to NSString can directly use its extended functions.

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.