Init Function And ID return type in Objective C
What is Init Function?
Init Function are used to initialize the instance variable
of class with starting value or initial value at time of creation of object. As
you heard about the constructor function in programming languages C,C++.
Constructor has name as the name of class. Constructor Function has no any
return type.
Example of constructor in C++: class student
{
private :
int StudentRollNo;
void Student()
{
StudentRollNo=101;
}
}
Creation of object is done below:
Student student1=new Student();
During this time it is called automatically or set to
initial value and that can be manipulate further accordingly.
Think for some time that init function is working in same
way like constructor.
Now come into Objective C Part.
In an Objective C init function has always id data type or
return type.The init function return type is not specific to integer,string due
to its initialization at run time what value will user enter these are not sure
or not known to compiler therefore id return type is used. Also Objective C is
dynamic language.
We come lator on it first solve the simple example of init
function.
In Student.h class
@interface Student:NSObject
{
NSString *studentname;
}
@property(nonatomic,strong) NSString *StudentName;
-(id)initWithStudentName:(NSString *)StudentName;
-(void)printName;
@end
In Student.m class
@implementation Student
@synthesize StudentName=_StudentName;
-(id)initWithStudentName:(NSString *)StudentName
{
studentname=StudentName;
return self;
}
-(void)printName
{
NSLog(@”The name of student is %@”,studentname);
}
@end
In main section
Int main(int argc,const char * argv[])
{
NSAutorealesePool *pool=[[NSAutorealesePool alloc]init];
Student student1=[Student alloc]initWithStudentName:@”abc”];
[student1 showInformation];
[pool drain];
Return 0;
}
id type in Objective C is collection of all information of
data types like NSString,NSInteger etc. in one type.When you using type of
object as id data type then you are not sure about the type of data you entered
or what a method could return. If you use another type instead of id then at
compile time compiler compile it but run time it causing big error of different
type.Type Conversion are possible but if you do not know type then how you convert.
In general you can say Objective C is Dynamic Language .A
call to specific type return at run time if you do not know about type.
Example:
In Student.h class
@interface Student
{
Id *studentname;
Id *studentid
}
@property(nonstomic,strong)id *StudentName;
@property(nonatomic,strong)id *StudentId;
-(void)StudentName:(NSString *)StudentName;
-(void)StudentID:(NSInteger *)StudentID;
@end
In Student.m class
@implementation Student
@synthesize StudentName=_StudentName;
@synthesize StudentID=_StudentID;
-(void)StudentName:(NSString *)StudentName
{
studentname=StudentName;
NSLog(@”The student name is:%@”,studentname);
}
-(void)StudentID:(NSInteger *)StudentID
{
studentid=StudentID;
NSLog(@”Student ID is :%d”,studentid);
}
Int main(int argc,const char * argv[])
{
NSAutorealesePool *pool=[[NSAutorealesePool alloc]init];
Student student1=[[Student alloc]init];
[student1 printStudentName:@”abc”];
[student1 printStudentID:101];
[pool drain];
Return 0;
}
Now in above case the type of data to StudentName instance
variable is NSString but id uses means automatically invoke at run time and
compiler nothing knows about it.
I hope you like my tutorial.
Ok lets we meet in our next tutorial.
My next tutorial will
be Dynamic Binding.
No comments:
Post a Comment