IOS syntax-category and inheritance

Source: Internet
Author: User

In Objective-c, there are two ways to extend a class to another method: category and inheritance.
1. Inheriting inherit
This is an attribute of the object-oriented language, and subclasses inherit the methods and properties of the parent class.
You cannot use categories for the following cases, you must use inheritance.

    1. The newly extended method has the same name as the original method, but it also requires the implementation of the parent class. Because a category is used, the implementation of the original class is overwritten and the original method cannot be accessed.
    2. Extended class properties, this category cannot be done.

Example code:

//   ViewControllerEx.h@interface  viewcontrollerex:uiviewcontroller//  The method you need to add @end//  viewcontrollerex.m@implementation   Implementation of the Viewcontrollerex// method @end

2 category
This is a feature of the Objective-c language, which can extend the method of the class without changing the class name and the implementation of the original class. Category cannot be used to add an instance variable to an extended class.

Example code:

// There is an established rule that when a category file is named, it is the original class name + the extended identity name //   nsstring+ex.h@interface  nsstring (ex)//  Extended Class-back method @end //   nsstring+ex.m@implementation  nsstring (ex)//  method implementation @end


3, the following two ways to use the last category.

    1. For some of the classes provided by the system, such as the Nsstring,nsarray,nsnumber class, the system itself does not advocate the use of inheritance to extend the method, because the internal implementations of these classes have limitations on inheritance, so the last use of the class to extend the method.
    2. Categories allow developers to group related methods into separate files for classes they build, for large and complex classes, which helps to improve maintainability and simplifies the management of individual source files.

IOS syntax-category and inheritance

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.