Objective-c Study Notes-Part 6-category and extension

Source: Internet
Author: User

Category allows you to extend the functions of a class even without the class source code and add methods to it.

** This can be understood as a mechanism of "Mixin.

The main function is to reuse the same method in a class without inheritance.

To use this method, you need to name the method at the category in the @ interface file of the class and write it in the corresponding implementation file.Code.

However, you cannot use category to add new instance variables to the class.

Category is only a compilation method, and the methods defined in it are no different from the instance methods of the class at runtime.

# Import "classname. H"
 
@ Interface classname (categoryname)
// Method declarations
@ End

Because category may need to use the instance variables of the class, you need to import the interface file of the class it extends.

# Import "classname + categoryname. H"
 
@ Implementation classname (categoryname)
// Method Definitions
@ End

The name of the category implementation file is marked as above.

The relationship between category and classes is many-to-one, that is, a class can have multiple extensions of different method sets, but a category can only be applied to one class (to be confirmed)

Typical application scenarios of category:
1. Provides class extension for the Framework (without the source code, it cannot be modified ).
2. If you do not want to generate a new subclass, such as the nsarray extension.
3. To facilitate project management, you can share a source code in multiple places or perform method version management, collaborative development by multiple people, and replace the public version with the local version.

It is not recommended to overwrite the methods in the class in category, because the methods in the category cannot call the superclass method (because there is no metadata support)

The category method cannot overwrite other category methods of the same class. Due to improper prediction of their loading priorities, errors may occur during compilation.

Overwrite the category method of the class library to change the behavior of the entire class library. Therefore, the classes that call those methods do not know that the implementation of the methods has changed.

Warning:
Although category is not limited to any class, it is not recommended to write a category for rootclass because it has a large impact and other developers may encounter problems if they do not pay attention to it.

In addition, class objects may also call these methods. Even when calling, the self pointer is not an instance but the class object itself.

Extensions
Class extension can be treated as an anonymous category.

So what is the use? the method defined in it cannot be overwritten by other category, because the reference cannot be found. Use this to implement private method in a certain sense.

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.