Objective C Feature : Categories


 CATEGORIES



Category is the one of the great feature in Objective C.Categories help keep code clean and less cluttered by eliminating the need for unnecessary subclassing.Categories provide a better approach to add some extra functionality to a class.


Categories allow us to add methods to an existing class, so that all instances of that class in your application gain your functionality. For example, say we have 100 NSString objects in your app, but you’d like to make a custom subclass so each NSString has an extra method (reverseString for example). With categories we can simply add the method in a category and all instances will be allowed to use the new method. The syntax is obviously slightly different from subclassing and categories don’t allow you to use instance variables. However, it is possible to overwrite a method already in place, but this should be done with caution and only if doing so is really necessary.

Syntax:

Categories follow the same syntax layout as a class, as in they have an implementation and an interface. 
The interface looks like this:

   @interface ClassName(category)
   // method Declaration(s)
      @end
The implementation look like this:
   @implementation ClassName(category)
   // method definition(s)
      @end

EXAMPLE:

Step 1: Set Up Your Project

Open  Xcode and click File > New > Project. Choose an iOS Single View Application from the window and click "Next." Name your product "Categories" and enter a name for your Company Identifier, such as "com.companyName.categories." Choose the iPhone device family and click "Next." Choose a location to store your project and click "Create."





Step 2: Create the Category

Note:This method trim the special characters from string(trim means removing special characters from the begin & end of string)
In Step1 we completed project setup, let's create a category that adds additional functionality to the NSString class. 
Goto Menu bar- File > New > File and choose a Cocoa Touch Objective-C category from the window. Click "Next." Give your category name as "TrimSpecialCharacters" and select NSString from the "Category on" drop down menu (you may need to type this in manually). Click "Next" followed by "Create."


Declare The Category Method:

Goto project Navigator then open the "NSString+TrimSpecialCharacters.h" class header File,add the following code to declare method.

@interface NSString (RemoveSpecialCharacters)
- (NSString *)trimSpecialCharactersFromString:(NSString *)string;
@end

Implement The Category Method

Open "NSString+TrimSpecialCharacters.m" to view the category's implementation file. Add the following code to create a method that will remove all the specialCharacters from an NSString. First we define an NSCharacter Setof the special Characters which we'll use as a reference to compare against the original input string.

-(NSString *)trimSpecialCharactersFromString:(NSString *)string{
    NSCharacterSet *specialCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"!@#$%^&()_{},<>:?.[]"];
 string = [string stringByTrimmingCharactersInSet:specialCharacterSet];
    return string;
}


Step 3: Import the Category

Click "ViewController.h" and import the category by adding the following code.
// ViewController.h

#import <UIKit/UIKit.h>
#import "NSString+TrimSpecialCharacters.h"
@interface ViewController : UIViewController

@end
Step 4: Test the Category

UI Design:

Lets design our View for testing the category. Click "MainStoryboard.storyboard" ,drag two Labels,One Button,one Textfield from object Library and arrange them like this.



IBOutlets and IBActions

Click on Assistent Editor Located at top right of Xcode. 
Give IBOutlet connections for Textfield, Label. Name it as "inputString" and "OutputString" respectively.
-IBAction for Button ,name it as "trimSpecialCharactersPressed'.
Now open ViewController.m implement the following method like this.


- (IBAction)trimspecialCharacterPressed:(id)sender {

  // Get the user entered text from textfield,store it into the string
   NSString *stringWithSpecialCharacters= self.inputString.text;
  // use the category method on the string.
  // output the trimmed string to the console and label named "outputString".
  self.outputString.text=[stringWithSpecialCharacters trimSpecialCharactersFromString:stringWithSpecialCharacters];
  NSLog(@" the string after triming is %@",self.outputString.text);
}




Now  run the application ,enter the string with special characters at the begin and end
Click on  "Trim Special Character" button ,then you will get trimmed string.

Note:this method trim only the SpecialCharacters entered in the CharacterSet.






No comments:

Post a Comment