Classification in the Objective-c

Source: Internet
Author: User
Tags definition class definition extend naming convention

Using the taxonomy in Object-c is a compile-time tool that allows us to extend 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. This is a bit like using a stereotype 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 definition and use of the classification of the example program, through the following code, we can add to the object-c nsstring camelcasestring classification, using the Camelcasestring method, you can remove a string of spaces, and rewrite the words after the original space into uppercase (the string is converted to a hump).

#import <Foundation/Foundation.h>/* The process of defining a classification can be roughly divided into the following steps: First, create a new file with an interface, that is, create an existing class     
The second step, in the new file to add the need to extend the method and the implementation of the method, that is, the need to add classification * *//nsstring represents the class name that will be added to the category, which must already exist.     
CamelCase is the name of the method added for the class.     
Only methods can be added and variables cannot be added.     

Header file naming convention: classname+categoryname.h @interface NSString (CamelCase)-(nsstring*) camelcasestring; @end @implementation NSString (CamelCase)-(nsstring*) camelcasestring {//Call NSString internal method to get the hump     
    String.     
    Self points to the class that is added to the category.     
             
    NSString *castr = [self capitalizedstring];     
    Create an array to filter out spaces and combine characters with delimiters. Nsarray *array = [castr componentsseparatedbycharactersinset: [Nscharacterset whitespacecharact     

    Erset]];     
    The character output of the array is nsstring *output = @ "";     
    For (NSString *word in array) {output = [output STRINGBYAPPENDINGSTRING:WORD];   return output;  
             
@end int main (int argc, const char * argv[]) {Nsautor     
             
    Eleasepool * Pool = [[NSAutoreleasePool alloc] init];     
    NSString *str = @ "My name is Bill.";     
    NSLog (@ "%@", str);     
    str = [STR camelcasestring];     
             
    NSLog (@ "%@", str);     
    [Pool drain];     
return 0; }

This article is from the "Late Evening" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/1153613

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.