Initializers and getter,setter in Swift
Learn Objective C from here:Click here
Learn Swift from here:Click here
Learn IOS from here With Objective C:Click here
As you have read in my last tutorials of Constants,classes,methods,objects in swift. Have you not read it: Click here.
Initializers in Swift is same as Objective C like initialise the properties,variables at the time of allocation of object or creating objects.
In objective c you need to write lot of code to initialise like below:
during declaration:
@property(nonatomic,strong)NSString *studentname;
-(id)initWithName:(NSString *)StudentName;
-(void)printDetails;
during implementation:
@synthesize studentname=_studentname;
-(id)initWithName:(NSString *)StudentName
{
self.studentname=StudentName;
}
-(void)printDetail
{
NSLog(@"The name of student is:%@",self.studentname);
}
during creating object
Student *student1=[[Student alloc]initWithName:@"Suraj"];
[student1 printDetail];
Now in Swift it is too easier just write code like below:
Have you read my last tutorials of swift of creating classes and objects or not: click here
Student.Swift
class Student
{
var studentname:String
init(StudentName:String)
{
self.studentname=StudentName
}
func printDetail()->Void
{
println("The name of student is:\(student name)")
}
}
Now create objects:
var student1=Student(StudentName:"Suraj")
student1.printDetail()
You see how easy to create object in swift.In general put in your mind that in init you are initialised property during creation.
Here studentname is property and call by self and StudentName is Arguments.
Now look for getter and setter method for computed property.It is easy to write if you have C# background where get and set keyword is used to create getter and setter methods.But do not worry you can easily learn. it is shown below:
In Objective C you can set and get property by Automatically generated by compiler as well as according to needs.
Also in Swift same thing if you do not write anything then getter and setter method generated automatically if you want it manually you can do by following like below:
class Student
{
var studentname:String
init(StudentName:String,physics:Float,chemistry:Float,math:Float)
{
self.studentname=StudentName
self.Physics=physics
self.Chemistry=chemistry
self.Math=math
TotalMarks=self.Physics+self.Chemistry+self.Math
}
var Physics:Float
var Chemistry:Float
var Math:Float
var TotalMarks:Float
var Percentage:Float
{
get
{
return TotalMarks/300*100
}
set
{
self.Percentage=0
}
}
func printDetail()->Void
{
println("The name Of Student is:\(studentname) and his Percentage is:\(Percentage)")
}
}
var student1=Student(StudentName: "Suraj",physics:90,chemistry:90,math:88)
student1.printDetail()
I hope you like my tutorials.If you want to donate can pay me at above link through Paypal.
Any query can comment here or mail me i will solve it.
My next Tutorial will be Take Deeper into the Programming of Swift with Concept of Sub Class.
Learn Objective C from here:Click here
Learn Swift from here:Click here
Learn IOS from here With Objective C:Click here
As you have read in my last tutorials of Constants,classes,methods,objects in swift. Have you not read it: Click here.
Initializers in Swift is same as Objective C like initialise the properties,variables at the time of allocation of object or creating objects.
In objective c you need to write lot of code to initialise like below:
during declaration:
@property(nonatomic,strong)NSString *studentname;
-(id)initWithName:(NSString *)StudentName;
-(void)printDetails;
during implementation:
@synthesize studentname=_studentname;
-(id)initWithName:(NSString *)StudentName
{
self.studentname=StudentName;
}
-(void)printDetail
{
NSLog(@"The name of student is:%@",self.studentname);
}
during creating object
Student *student1=[[Student alloc]initWithName:@"Suraj"];
[student1 printDetail];
Now in Swift it is too easier just write code like below:
Have you read my last tutorials of swift of creating classes and objects or not: click here
Student.Swift
class Student
{
var studentname:String
init(StudentName:String)
{
self.studentname=StudentName
}
func printDetail()->Void
{
println("The name of student is:\(student name)")
}
}
Now create objects:
var student1=Student(StudentName:"Suraj")
student1.printDetail()
You see how easy to create object in swift.In general put in your mind that in init you are initialised property during creation.
Here studentname is property and call by self and StudentName is Arguments.
Now look for getter and setter method for computed property.It is easy to write if you have C# background where get and set keyword is used to create getter and setter methods.But do not worry you can easily learn. it is shown below:
In Objective C you can set and get property by Automatically generated by compiler as well as according to needs.
Also in Swift same thing if you do not write anything then getter and setter method generated automatically if you want it manually you can do by following like below:
class Student
{
var studentname:String
init(StudentName:String,physics:Float,chemistry:Float,math:Float)
{
self.studentname=StudentName
self.Physics=physics
self.Chemistry=chemistry
self.Math=math
TotalMarks=self.Physics+self.Chemistry+self.Math
}
var Physics:Float
var Chemistry:Float
var Math:Float
var TotalMarks:Float
var Percentage:Float
{
get
{
return TotalMarks/300*100
}
set
{
self.Percentage=0
}
}
func printDetail()->Void
{
println("The name Of Student is:\(studentname) and his Percentage is:\(Percentage)")
}
}
var student1=Student(StudentName: "Suraj",physics:90,chemistry:90,math:88)
student1.printDetail()
I hope you like my tutorials.If you want to donate can pay me at above link through Paypal.
Any query can comment here or mail me i will solve it.
My next Tutorial will be Take Deeper into the Programming of Swift with Concept of Sub Class.

No comments:
Post a Comment