Swift is a language developed by Apple which is made using modern approach, include safety features and software design patterns. As the name suggests swift is fast, safe and easy to use. It is basically made a replacement for c-based family(C, C++ and Objective-C). It can be used to make apps and can be also used for cloud services and it is among the fastest growing languages.
As the name suggests, values that can be varied later according to needs.
Variables and Constants are like containers for storing values. Let's say i have a box in which i stored a number 10, but later on i wished to change it to let's say 6 then what i will do is take the old value out and put a new value inside that box.
But not the same happens in case of Constants. As their name suggests, their value remain constant over time and cannot be changed. Here if is stored 10 then it's gonna remain 10 and cannot be changed.
Comments in Swift : We use
// for comments in swift.Comments are like if we do not want to include something in our code but we want it to be present in our code snippet. So compiler while compiling will ignore these comments.
They are defined with keyword
var variableOne = 10 // assigned value of 10 to a variable // Swift has a cool feature of type inference that means that swift will automatically decides the type of variable for you. Like in above line swift will say okay this is variable of type Integer var stringOne = "Hello, World!" // Here swift will automatically infer the type of variable as String var doubleOne = 1.00 //Here Swift will infer this variable as of type Double var boolOne = true //Here Swift will infer the type of variable as type Bool
var x = 1 x = 2 print(x) // this will print value of x as 2
They are defined using keyword
let x = 1 x = 2 // Swift will give error here as x is declared as constant in above line
Type Annotation - In above part we saw that Swift has power to infer the type of variable but what if we want to play safe and declare type of variable ourselves.
var a : Int // Here we are saying that we have variable a of type Int var b : Double // Here we are saying that we have variable b of type Double var c : String // Here we are saying that we have variable c of type String var d : Bool // Here we are saying that we have variable d of type Bool
Note : Semicolons after end of every statement are optional in Swift.
This was all about Variables and Constants in Swift. I haven't included data types in detail but we will see that in next post.