Constants,Properties,Classes,Objects,Methods in Swift
Before you going further learn swift tutorials from here: Click here
Read Data Types in Swift from here:Click here
Learn Objective C from Here:Click here
Learn Custom Cell in Table View Using Storyboard from here:Click Here
Constants in Swift:
You have studied in my last tutorial how easy is to write variables in swift and what are the data types that a swift contain.
Now the Most important thing comes how to write constants in Swift. Do you know about constants. Constants are the permanent stored value in memory address once initialised can not be changed further.
In Objective C You need to write pointer assigning of Constants with Const keyword.
NSString *const StudentName=@"ABC";
In Swift it is written as
let StudentName="ABC"
I already describe in my last tutorial how to write Strings in swift.
If you want to learn:click Here
In Swift There is not pointer assignment.In general, Swift language has not direct assignment of pointer to memory which makes it more safer.
Properties in Swift:
Objective C has two types of file Header and implementation file.
In swift You have only one file with .Swift You need to linked with that how to write methods and properties.
As you have studied in my Objective C tutorials Properties are those backed up by instance variables. In swift Properties are not backed up with instance variables.Each Property and instance variables has only one locations in memory.
In Swift Properties are declared in same way as Variables but having variables are written in inside Methods but Properties are declared in a Class.
In Objective C Properties are written as in class interface.
@property(nonatomic,strong) NSString *firstname;
In Swift Properties are written as:
Class ...
{
Var first name="ABC"
}
Above Property is initialised at the time of declaration.
Like Objective C You can create getter and setter methods for setting property value.
In Objective C
Getter and setter can be generated automatically by Compiler through the
@syntheisize firstname=_firstname;
In swift it is Different you can set with get and set keyword like below.
If you have C# programming background you have seen for making Business Object Get and set methods are used to assign Value to property.
class ...
{
var firstname
{
get
{
return self.first name;
}
set
{
self.firstname="ABC"
}
}
}
There are two types of Properties in Swift. One is Computed where you manually set getter and setter methods and other is Normal variable like properties. In computed property you need to write the data type explicitly.
Lets look a simple example of x and y numbers
class ....
{
var x=10
var y:Int
{
get
{
return (x+10)
}
set
{
x=10
}
}
}
Methods in Swift:
Methods in Objective C has very difficult remember implementation. But now in swift solve this problem just simple like function implementation of Javascript.
In objective C methods is written as
-(return type)methodsname:(datatype of first arguments)FirstArguementsName SecondArguements:(datatype of second arguments)SecondArguementName
{
//body of function
}
In Swift the method is written as:
func(keyword) function name()->returnType
{
//body of function
}
When arguments are passed then function look like
func setName(firstname:string,lastname:string)->void
{
println("The name of Student is:\(first name,last name)")
}
But in objective c you need to write
-(void)setName:(NSString *)firstname lastname:(NSString *)Lastname
{
NSLog(@"The name of student is:%@%@",first name,second name);
}
Classes in Swift:
Classes designing in Swift is Similar like Objective C .
In swift class is declared as
class classname
{
//property
//methods
}
In swift there is only one file with .swift extension.Implementation and Declaration is done in only one file.
Simple look up at Objective C for class declaration
In Student.h
@interface Student:NSObject
@property(nonatomic,strong)NSString *StudentName;
-(void)printName:(NSString *)StudentName;
In Student.m
@implementation Student
@syntheisize StudentName=_StudentName;
-(void)printName:(NSString *)StudentName
{
NSLog(@"The name of student is: %@",student name);
}
In Student.swift
class Student
{
var StudentName
{
get
{
return self.StudentName
}
set
{
self.StudentName
}
}
func printName(studentname:String)->void
{
println("The name of student is:\(student name)")
}
}
Objects in Swift:
Creation of objects in Swift is very easy like Objective C.
In Objective C Object is created like that:
Student *student1=[[Student alloc]init];
In Swift Object is created like that:
var Objectname=classname()
var student1=Student()
Calling of methods of class can be done through simple dot operator in swift.
student1.printName()
but in Objective C it can be done through square brackets like:
[student1 printName];
I have not used init yet so above tutorials create error . So read next tutorial of initialise the property so to run this simple example.
I hope you like my this tutorials. If you have any query can mail me or comment here.
My next Tutorial will be init in Swift.
Before you going further learn swift tutorials from here: Click here
Read Data Types in Swift from here:Click here
Learn Objective C from Here:Click here
Learn Custom Cell in Table View Using Storyboard from here:Click Here
Constants in Swift:
You have studied in my last tutorial how easy is to write variables in swift and what are the data types that a swift contain.
Now the Most important thing comes how to write constants in Swift. Do you know about constants. Constants are the permanent stored value in memory address once initialised can not be changed further.
In Objective C You need to write pointer assigning of Constants with Const keyword.
NSString *const StudentName=@"ABC";
In Swift it is written as
let StudentName="ABC"
I already describe in my last tutorial how to write Strings in swift.
If you want to learn:click Here
In Swift There is not pointer assignment.In general, Swift language has not direct assignment of pointer to memory which makes it more safer.
Properties in Swift:
Objective C has two types of file Header and implementation file.
In swift You have only one file with .Swift You need to linked with that how to write methods and properties.
As you have studied in my Objective C tutorials Properties are those backed up by instance variables. In swift Properties are not backed up with instance variables.Each Property and instance variables has only one locations in memory.
In Swift Properties are declared in same way as Variables but having variables are written in inside Methods but Properties are declared in a Class.
In Objective C Properties are written as in class interface.
@property(nonatomic,strong) NSString *firstname;
In Swift Properties are written as:
Class ...
{
Var first name="ABC"
}
Above Property is initialised at the time of declaration.
Like Objective C You can create getter and setter methods for setting property value.
In Objective C
Getter and setter can be generated automatically by Compiler through the
@syntheisize firstname=_firstname;
In swift it is Different you can set with get and set keyword like below.
If you have C# programming background you have seen for making Business Object Get and set methods are used to assign Value to property.
class ...
{
var firstname
{
get
{
return self.first name;
}
set
{
self.firstname="ABC"
}
}
}
There are two types of Properties in Swift. One is Computed where you manually set getter and setter methods and other is Normal variable like properties. In computed property you need to write the data type explicitly.
Lets look a simple example of x and y numbers
class ....
{
var x=10
var y:Int
{
get
{
return (x+10)
}
set
{
x=10
}
}
}
Methods in Swift:
Methods in Objective C has very difficult remember implementation. But now in swift solve this problem just simple like function implementation of Javascript.
In objective C methods is written as
-(return type)methodsname:(datatype of first arguments)FirstArguementsName SecondArguements:(datatype of second arguments)SecondArguementName
{
//body of function
}
In Swift the method is written as:
func(keyword) function name()->returnType
{
//body of function
}
When arguments are passed then function look like
func setName(firstname:string,lastname:string)->void
{
println("The name of Student is:\(first name,last name)")
}
But in objective c you need to write
-(void)setName:(NSString *)firstname lastname:(NSString *)Lastname
{
NSLog(@"The name of student is:%@%@",first name,second name);
}
Classes in Swift:
Classes designing in Swift is Similar like Objective C .
In swift class is declared as
class classname
{
//property
//methods
}
In swift there is only one file with .swift extension.Implementation and Declaration is done in only one file.
Simple look up at Objective C for class declaration
In Student.h
@interface Student:NSObject
@property(nonatomic,strong)NSString *StudentName;
-(void)printName:(NSString *)StudentName;
In Student.m
@implementation Student
@syntheisize StudentName=_StudentName;
-(void)printName:(NSString *)StudentName
{
NSLog(@"The name of student is: %@",student name);
}
In Student.swift
class Student
{
var StudentName
{
get
{
return self.StudentName
}
set
{
self.StudentName
}
}
func printName(studentname:String)->void
{
println("The name of student is:\(student name)")
}
}
Objects in Swift:
Creation of objects in Swift is very easy like Objective C.
In Objective C Object is created like that:
Student *student1=[[Student alloc]init];
In Swift Object is created like that:
var Objectname=classname()
var student1=Student()
Calling of methods of class can be done through simple dot operator in swift.
student1.printName()
but in Objective C it can be done through square brackets like:
[student1 printName];
I have not used init yet so above tutorials create error . So read next tutorial of initialise the property so to run this simple example.
I hope you like my this tutorials. If you have any query can mail me or comment here.
My next Tutorial will be init in Swift.
No comments:
Post a Comment