Tuesday, 9 June 2015

Category in Objective C

Category In Objective C

Before going furthor to categories please read my previous tutorial
Link is here :Previous Tutorial

Categories are the way to expand the exisiting class with adding certain messages required at some situations not at all time need messages.
 Do not think that it is like inheritance  Where reusability of code occur if you do not know about it click here: Inheritance Tutorial

You can also add your own method to Cocoa Touch classes which are not modified.These are the original classes made by apple like NSString,NSInteger etc. Additional method required at some situations you can add accordingly.In General you can say that categories in objective c are the way to separate usefull information from that information which are needed in certain situations.

General Syntax:

@interface ClassName (Category Name)


And it will save with ClassName+CategoryName.h or .m
Also it has include #import "Classname+CategoryName.h" file where you want to include additional methods.Category applies to Base class also added to derived classes.

Let we solve simple example of Student in which Head of Department in College check sports participation of student in class.
I have use XCode 6 You can use according to your preference but the way to add category will be different.
First Add Category file in Objective C How to add it see Below Screenshot







Then Write the name of your Category do you want to create and check the Class for which additional method is added.
The method will be added in Student+StudentSportsDisplay Below is screensheet show it.







In Student.h

@interface Student : NSObject
{
    NSString *studentname;
    NSString *sportsname;
}
@property(nonatomic,strong)NSString *StudentName;
@property(nonatomic,strong)NSString *SportsName;
-(id)initWithStudentName:(NSString *)StudentName Sports:(NSString *)SportsName;
-(void)printParticipation;

In Student.m

#import "Student+StudentSportsDisplay.h"
@implementation Student
@synthesize StudentName=_StudentName;
@synthesize SportsName=_SportsName;
-(id)initWithStudentName:(NSString *)StudentName Sports:(NSString *)SportsName
{
    studentname=StudentName;
    sportsname=SportsName;
    return self;
}
-(void)printParticipation
{
    NSLog(@"The Student Participate in sports %@",[self printSportsName]);
}

In Student+StudentSportsDisplay.h

@interface Student (StudentSportsDisplay)
-(NSString *)printSportsName;


In Student+StudentSportsDisplay.m

@implementation Student (SportsDisplay)
-(NSString *)printSportsName
{
    return [NSString stringWithFormat:@"%@",sportsname];
}


In Main Section
int main(int argc,const char *argv[])
{
NSAutorealse *pool=[[NSAutorealse alloc]init];
 Student *student1=[[Student alloc]initWithStudentName:@"abc" Sports:@"football"];
        [student1 printParticipation];
[pool drain];
return 0;
}

And the Result will be shown as







I hope you like my tutorial. If you have any query please comment here and mail me. Also Due to demand of some people the previous tutorial output will be updated as soon on those pages.Be updated on my Blog.

My Next Tutorial will be Extension in Objective C

 If You read this tutorial carefully then it easy to learn for you about extension.

No comments:

Post a Comment