Swift is another powerful and intuitive programming language by apple to make Apps for iOS, OS X, watchOS, and tvOS .
Swift adopts the best of C and Objective-C, without the constraints of C compatibility.
Apple developers designed the language keeping some goals in mind:
1)Faster coding
2)Faster execution
Why should you choose Swift as a programming language ?
1)Swift follows safe programming patterns
2)Swift includes latest features for easy programming,flexibility and more fun.
3)The syntax of Swift programming language was designed to be clean and easy to read.
4)Swift provides syntax similar to Objective C
5)Swift provides playground feature where the iOS developers can see the result of execution after writing their code.
6)Swift is based upon some great ideas from various other popular languages such as Objective-C, Rust, Haskell, Ruby, Python, C#, and CLU.
7)Swift will not only supports Objective-C ,but it will also replace C for embedded programming on Apple platforms.
How to declare variables in Swift ?
Declaring a variable tells the compiler where and how much to create the storage for the variable.
If you want to use variables in Swift,you should declare them using var keyword:
Example:
How to declare an integer variable in Swift ?
You should use Int instead of Integer
var value:Int
value = 2
Declaring Variable in one line:
var value:Int = 2
Using Type inference
var value = 2
Swift has feature called type inference where it can infer the type of the variable based on the value you assign to it.
How to declare String Variable in Swift ?
var quality : String = "best"
Note:
We can declare class level instance variables and local variables in methods in Objective C but in Swift,we can't declare class-level variables ,only local variables that we can declare within methods.
We have can create a class-level instance variable by declaring properties.
How to declare constant Variables in Swift?
Constants are only meant to be used with values that will never change.If you edit the constant variable,it will through an error.
let result = 42 //Constant
let is the type for a Swift constant.You must always initialize your let constants, even if you explicitly type them
Example:
What happens if we try to change Swift constant.
let value = 42
value = 21//Error: Cannot assign to 'let' value 'value'
Read:
Swift Interview Questions And Answers part 1