iOS categories category and extension extension

Source: Internet
Author: User

This article is mainly about iOS category and extension (Extension, anonymous category), please visit the website of Crazy software education. Category is used very frequently in iOS development. Especially in the expansion of the system class, we can not inherit the system class, directly to the system class to add methods, the maximum extent of the objective-c dynamic language characteristics.

[OBJC] View plaincopy on code to see a snippet derived from my Code slice

#import

@interface NSObject (Category)

-(void) MyMethod;

@end

This is the simplest category to use for the NSObject class and adds a method to NSObject.

Points to note in using Category:

(1) The method of category does not have to be implemented in @implementation, but it can be implemented in other locations, but when the method of category is called, the implementation of the method is not found according to the inheritance tree, and the program crashes.

(2) Category theory cannot add variables, but @dynamic can be used to compensate for this deficiency.

[OBJC] View plaincopy on code to see a snippet derived from my Code slice

#import

static Const voidvoid * Externvariablekey =&externvariablekey;

@implementation NSObject (Category)

@dynamic variable;

-(ID) variable

{

Return Objc_getassociatedobject (self, externvariablekey);

}

-(void) SetVariable: (ID) variable

{

Objc_setassociatedobject (self, externvariablekey, variable,

Objc_association_retain_nonatomic);

}

-----------------------------------------------------------

Extension is very much like an unnamed category, that is, an anonymous category

[OBJC] View plaincopy on code to see a snippet derived from my Code slice

@interface Myclass:nsobject

@property (retain, readonly) float value;

@end

In general, extension are placed above the @implementation in the. m file.

@interface MyClass ()

@property (retain, readwrite) float value;

@end

Points to note when using extension:

(1) The method in extension must be implemented in the @implementation, otherwise the compilation will error.

Category and extension are a little different.

The category function adds a method to a class that does not have source code, in the format: Define a pair of. h and. m

Extension all methods of the management class, format: Write the code into the. m file of the original class

Crazy software Java training keep up with the latest Java Power Flow technology, the first in the country to explain the new Java 8 features, according to the needs of enterprise work in the development of technology, so that students learn the curriculum is the most needed practical technology, but also to ensure that students can learn these skills point, learn, and crazy software education courses in real-time updates, With the rapid development of information technology, students can be guaranteed to learn to update the more mainstream technology. Teachers are strong, the teacher has more than 8 years of project experience, Crazy software Java course is the combination of theory and practice, so as to ensure that the students have a good grasp of theoretical knowledge at the same time, can also be based on theoretical guidance to do practical work. In order to cultivate students ' practical ability, many practical courses have been set up, in which students can experience the real process of actual project, so as to master knowledge more deeply. For more information, please contact QQ:544627560 Hotline: 020-28309358 Consulting qq:707552864 can visit the Crazy Software education website to inquire. Search Number: Crazy software, participate in the 2015 offer, have access to coupons and vouchers coupons.

-font-family: "Times New Roman" > Annual offer with access to coupons and vouchers coupons.

iOS categories category and extension extension

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.