iOS categories and extensions-Chaoxiaopo

Source: Internet
Author: User

Category

@interface ClassName (CategoryName)
Methoddeclarations
@end

category is used very frequently in iOS development . < Span style= "color: #ff0000;" In particular, when it comes to extending the system classes, we can add methods to the system classes without inheriting the system classes, maximizing the dynamic language characteristics of objective-c .

Precautions

1: A category is an extension of a class , only methods can be declared, and variables cannot be declared. Also, class methods have higher precedence and can override the original class method.

2: The Super method cannot be called in a class method. --The limitations of categories

3: Thecategory method cannot be overridden in other category methods in the same class. Because they do not anticipate their load precedence, they may have errors at compile time.

Use Category:

1. Provides the extension of the class to the framework (no source code, cannot be modified).

2. If you do not want to generate a new subclass, such as an extension to Nsarray.

3. Easy to do project management, can be a source in multiple places to share or do the method version management, multi-person collaborative development, with the local version of the replacement of the public version of the implementation.

Extended:

1: In some cases, we need to declare a @property, which is read-only (ReadOnly) and internally readable (ReadWrite), which can be achieved by extensions

@interfaceBaseclass:nsobject@property (READONLY) NSString *privatestring;@end//. m@interface BaseClass () @property (readwrite) NSString *< Span style= "margin:0px; padding:0px; font-family: ' Courier new '; >privatestring;  @end  @implementation  Baseclass @synthesize  Privatestring;//... @end 

2: Enables the editor to better validate private methods or variables of a class

  @interface void @end  @implementation  Baseclass//... @end           
&NBSP;    

iOS categories and extensions-Chaoxiaopo

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.