Protocol in Objective C
Before going to this tutorial please read my previous tutorial Link is here:Click here
Protocols are different from category and extension if you have not read it then Click here
Protocols is defined as set of related methods in Objective C. In Other Words a good programmer is that one who uses all the benefits of Objective C or in any other language. Also you can say it is the way to arrange related method in objective c all those method which are linked in relation. In IOS UITableViewDataSource is a protocol of uitableview class having contain collection of related methods. Like number of rows in section etc. We come into detail of IOS lator first clear Objective C Concepts.
Syntax to declare Protocol
@Protocol Protocolname
@end
You can add methods,properties in protocol and used in any class where header file is imported.
You can also use optional methods in objective c by using @optional and all methods are optional untill @required keyword is not found.
lets look up the example of Protocol in Objective C
Here StduentDetail is protocol.
First go to add new class like show below
In StduentDetail.h file protocol file
@protocol StudentDetail <NSObject>
@property(nonatomic,strong)NSString *ParentsName;
@property(nonatomic,strong)NSString *SportsName;
@property(nonatomic,strong)NSString *TotalFees;
@property(nonatomic,strong)NSString *otherDetail;
@property(nonatomic,strong)NSString *listOfSubject;
-(void)printBackgroundDetail:(NSString *)ParentsName;
-(void)printSportsDetail:(NSString *)SportsName;
@optional
-(void)printFeesDetail:(NSString *)TotalFees;
-(void)printOtherActivities:(NSString *)otherDetail;
@required
-(void)printSubjectName:(NSString *)listOfSubject;
In Student.h file
#import "StudentDetail.h"
@interface Student : NSObject<StudentDetail>
{
NSString *studentname;
}
@property(nonatomic,strong)NSString *StudentName;
-(id)initWithStudentName:(NSString *)StudentName;
In Student.m File
@implementation Student
@synthesize StudentName=_StudentName;
@synthesize ParentsName=_ParentsName;
@synthesize TotalFees=_TotalFees;
@synthesize SportsName=_SportsName;
@synthesize listOfSubject=_listOfSubject;
-(id)initWithStudentName:(NSString *)StudentName
{
studentname=StudentName;
return self;
}
-(void)printBackgroundDetail:(NSString *)ParentsName
{
NSLog(@"The Student:%@ name of parents are: %@",studentname,ParentsName);
}
-(void)printSportsDetail:(NSString *)SportsName
{
NSLog(@"The Student: %@ name of sports is:%@",studentname,SportsName);
}
-(void)printFeesDetail:(NSString *)TotalFees
{
NSLog(@"The Student: %@ has total fees is:%@",studentname,TotalFees);
}
-(void)printSubjectName:(NSString *)listOfSubject
{
NSLog(@"The Student:%@ name of subject taken by student are:%@",studentname,listOfSubject);
}
In Main section
Student *student1=[[Student alloc]initWithStudentName:@"abc"];
[student1 printBackgroundDetail:@"xyz"];
[student1 printFeesDetail:@"$40000"];
[student1 printSportsDetail:@"Football"];
[student1 printSubjectName:@"Computer"];
Result is shown here
Source Code is available here : Source Code Download
I hope you like my tutorial. If you have any query you can comment here or mail me.
My next tutorial will be NSArray and NSDictionary Class
Before going to this tutorial please read my previous tutorial Link is here:Click here
Protocols are different from category and extension if you have not read it then Click here
Protocols is defined as set of related methods in Objective C. In Other Words a good programmer is that one who uses all the benefits of Objective C or in any other language. Also you can say it is the way to arrange related method in objective c all those method which are linked in relation. In IOS UITableViewDataSource is a protocol of uitableview class having contain collection of related methods. Like number of rows in section etc. We come into detail of IOS lator first clear Objective C Concepts.
Syntax to declare Protocol
@Protocol Protocolname
@end
You can add methods,properties in protocol and used in any class where header file is imported.
You can also use optional methods in objective c by using @optional and all methods are optional untill @required keyword is not found.
lets look up the example of Protocol in Objective C
Here StduentDetail is protocol.
First go to add new class like show below
Then Add protocol name here StudentDetail is protocol.
In StduentDetail.h file protocol file
@protocol StudentDetail <NSObject>
@property(nonatomic,strong)NSString *ParentsName;
@property(nonatomic,strong)NSString *SportsName;
@property(nonatomic,strong)NSString *TotalFees;
@property(nonatomic,strong)NSString *otherDetail;
@property(nonatomic,strong)NSString *listOfSubject;
-(void)printBackgroundDetail:(NSString *)ParentsName;
-(void)printSportsDetail:(NSString *)SportsName;
@optional
-(void)printFeesDetail:(NSString *)TotalFees;
-(void)printOtherActivities:(NSString *)otherDetail;
@required
-(void)printSubjectName:(NSString *)listOfSubject;
In Student.h file
#import "StudentDetail.h"
@interface Student : NSObject<StudentDetail>
{
NSString *studentname;
}
@property(nonatomic,strong)NSString *StudentName;
-(id)initWithStudentName:(NSString *)StudentName;
In Student.m File
@implementation Student
@synthesize StudentName=_StudentName;
@synthesize ParentsName=_ParentsName;
@synthesize TotalFees=_TotalFees;
@synthesize SportsName=_SportsName;
@synthesize listOfSubject=_listOfSubject;
-(id)initWithStudentName:(NSString *)StudentName
{
studentname=StudentName;
return self;
}
-(void)printBackgroundDetail:(NSString *)ParentsName
{
NSLog(@"The Student:%@ name of parents are: %@",studentname,ParentsName);
}
-(void)printSportsDetail:(NSString *)SportsName
{
NSLog(@"The Student: %@ name of sports is:%@",studentname,SportsName);
}
-(void)printFeesDetail:(NSString *)TotalFees
{
NSLog(@"The Student: %@ has total fees is:%@",studentname,TotalFees);
}
-(void)printSubjectName:(NSString *)listOfSubject
{
NSLog(@"The Student:%@ name of subject taken by student are:%@",studentname,listOfSubject);
}
In Main section
Student *student1=[[Student alloc]initWithStudentName:@"abc"];
[student1 printBackgroundDetail:@"xyz"];
[student1 printFeesDetail:@"$40000"];
[student1 printSportsDetail:@"Football"];
[student1 printSubjectName:@"Computer"];
Result is shown here
Source Code is available here : Source Code Download
I hope you like my tutorial. If you have any query you can comment here or mail me.
My next tutorial will be NSArray and NSDictionary Class



No comments:
Post a Comment