Objective-c categories and Extensions (Extension)

Source: Internet
Author: User


1. Classification (category)


Using classification in OBJECT-C is a compile-time tool that allows us to augment it by adding a method to a class (but not adding new instance variables through category), and we do not need to access the code in the class to do that. This is a bit like using a prototype in JavaScript to define attributes.



We can create a new method for a class without having to edit the class definition in code.



Here is the example of defining and using the classification of the program, through the following code, we can add camelcasestring classification to the NSString in Object-c, using the Camelcasestring method, you can remove the space in a string, and rewrite the words after the original space into uppercase (the string is converted to camel).


[CPP]View Plaincopy



#import <Foundation/Foundation.h>
/*
The process of defining a classification can be broadly divided into the following steps:
The first step is to create a new file with an interface that creates an existing class
The second step is to add the implementation of the methods and methods that need to be expanded in the new file, i.e. the classification to be added
*/
NSString indicates the class name that will be added to the taxonomy, and the class must already exist.
CamelCase is the name of the method that is added for the class.
You can only add methods, and you cannot add variables.
Header file naming convention: classname+categoryname.h
@interface NSString (CamelCase)
-(nsstring*) camelcasestring;
@end
@implementation NSString (CamelCase)
-(nsstring*) camelcasestring
{
//Call NSString's internal method to get the hump string.
//self points to the class to which the classification was added.
NSString *castr = [self capitalizedstring];
//Create an array to filter out spaces and combine characters with delimiters.
Nsarray *array = [Castr componentsseparatedbycharactersinset:
[Nscharacterset Whitespacecharacterset]];
//Put the character of the array out
NSString *output = @"";
For (NSString *word in array)
{
output = [Output STRINGBYAPPENDINGSTRING:WORD];
}
return output;
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * Pool = [[NSAutoreleasePool alloc] init];
NSString *str = @"My name is Bill.";
NSLog (@"%@", str);
str = [STR camelcasestring];
NSLog (@"%@", str);
[Pool drain];
return 0;
}




2. Extension (Extension)


As for the extension, you can understand this: the extension is an anonymous classification, but unlike anonymous classification, the extension can add new instance variables (does it feel that the extension is strong enough to be unusual?). ^_^)



It is recommended to use extensions in the. m file of a custom class since Xcode 4, which guarantees good code encapsulation and avoids exposing the private interface to the outside.



The following is an example of an extension:


[Plain]View Plaincopy


@interface Myclass:nsobject
-(float) value;
@end
@interface MyClass () {//Note here: Extended
float value;
}
-(void) SetValue: (float) newvalue;
@end
@implementation MyClass
-(float) value {
return value;
}
-(void) SetValue: (float) NewValue {
value = newvalue;
}
@end


Objective-c categories and Extensions (Extension)


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.