Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use Of Category in Objective-C

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 397
    Comment on it

                                                                                Use of Category in Objective-C

    Category in objective-C language is used to build extra methods which can be used by the user later when ever needed. These extra methods which are added with the help of category makes works more easier. This whole concept is used to extend the classes by adding extra methods which make work more easier and efficient. I will explain you this concept with the help of a program in which NSString class is used with category. So class name added with category name  will be given in first line.

    Here in below example first of all we have to add a file in Xcode which is of objective-c type followed by selecting as category sub class and mention name of category simultaneously NSString class which is the return type.In interface I will declare a method with return type NSString.So here is the example given below:-

     

    NSString+Categoryexample.h

    #import <Foundation/Foundation.h>
    
    @interface NSString (Categoryexample) //categoryname
    -(NSString *)addition:(NSString *)a; //method name
    @end

     

     

    After method declaration in interface now in implementation definition of the method will be given:-

    NSString+Categoryexample.m

    #import "NSString+categoryofnsstring.h"
    
    @implementation NSString (Categoryexample)
    -(NSString *)addition:(NSString *)a
    {
        NSString *str=[[NSString alloc]init]; //allocating memory to the object
        str=[NSString stringWithFormat:@"hiii %@",a]; //this will add prefix hii before the data provided in main method
        return str;
    }
    @end

     

    After defining the method in implementation part of a class now calling of method will be done in main method as given below:-

     

    main.m

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    #import "NSString+categoryofnsstring.h"
    
    int main(int argc, char * argv[])
    
    {
        @autoreleasepool {
            NSString *str1=@User; //assigning value to str1
            NSLog(@"%@",[str1 addition:str1]); //calling of method
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    }

     

    After debugging output will be shown as below:-


    output

    2016-02-24 17:42:35.270 Categoryinobjective[3220:126949] hiii User

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: