Object-c category, object-c category

Source: Internet
Author: User

Object-c category, object-c category

Category: adds a new method to a known class;

I. Category:

1. Application of category methods:

 

  • Extension of existing classes: for example, you can extend the classes in the Cocoa touch framework. The methods added to the category are inherited by the quilt class, and there is no difference between them at runtime and other methods.
  • As an alternative to subclass: you do not need to define or use a subclass. You can directly add methods to existing classes by category.
  • Classify methods in a class: Use catigory to divide a large class into small pieces for separate development, so as to better update and maintain the methods in the class.

2. Limitations of category methods:

 

  • You cannot add new instance variables to a category. The category does not have a location to hold instance variables. To add instance variables of a class, you can only define the subclass.
  • If the method of the successive classes is covered in the category. This will lead to the break of the super message, because the method of the category has a higher priority. Therefore, do not overwrite the methods in the existing class.

 

3. category definition:

Naming rules for a class: class name + extension method, such as "NSString + Revert ". The interface declaration of the category is very similar to the definition of the class, but the category does not inherit the parent class. It only needs to contain a bracket to indicate the main purpose of the class.

NSString + myobc. h file:

#import <Foundation/Foundation.h>@interface NSString (myobc)- (void)test;@end

NSString + myobc. m file:

#import "NSString+Revert.h"@implementation NSString (myobc)
- (void)test{ } @end

One thing about the Internet is the release of the latest Internet Information Platform www.yidin.net.

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.