Basic knowledge of iOS: categories, extensions, and ios categories

Source: Internet
Author: User

Basic knowledge of iOS: categories, extensions, and ios categories

1. Category: Add a new method for an existing class. However, you cannot add instance variables.

Application: 1. extend existing classes, for example, classes in the system. The methods added to the category are inherited by the quilt class, and there is no difference between them at runtime and other methods.

2. As an alternative to subclass, you do not need to define or use a subclass. You can directly add methods to existing classes through categories.

3. classify the methods in the class and 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.

4. What is different from a common interface is that the instance method in the Category implementation file does not need to implement all declared methods as long as you do not call it.

Limitations: 1. You cannot add new instance variables to a category.

2. Methods in a category have a higher priority. If a Category Contains methods of the original class (overload ). The super message is invalid. Therefore, do not overwrite the methods in the existing class.

If you do need to overload it, you can create a subclass by inheriting it.

Naming method: class name + extension method, such as "UIAlertView + AFNetworking ". 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 a bracket to indicate the main usage of the class.

AFNetworking has such a category to implement network requests with alterview.

For example: UIAlertView + AFNetworking. h file

<span style="font-family:System;font-size:12px;">#import <Foundation/Foundation.h>@interface UIAlertView (AFNetworking)+ (void)showAlertViewForTaskWithErrorOnCompletion:(NSURLSessionTask *)task delegate:(id)delegate;</span>
In the UIAlertView + AFNetworking. m file

<Span style = "font-family: System; font-size: 12px;"> # import "UIAlertView + AFNetworking. h "@ implementation UIAlertView (AFNetworking) + (void) Submit :( NSURLSessionTask *) task delegate :( id) delegate {[self defined: task delegate: delegate cancelButtonTitle: NSLocalizedStringFromTable (@ "Dismiss", @ "AFNetworking", @ "UIAlertView Cancel Button Title") otherButtonTitles: nil, nil];} </span> <span style = "font-size: 12px; font-family:, 'arial narrow', Arial, serif;"> </span>

2. Extension: the function of extension is to define your own private method.

The format is the same as the category. You do not need to create a new file. The methods defined in the extension are implemented in the @ implementation code area of the class.

When the category name is not provided during definition extension, the method defined in the extension is regarded as an API that must be implemented. In this case, if the method does not implement code, the compiler will warn that the implementation of the method must appear in the @ implementation code block of the class body.



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.