Sunday, January 15, 2017

Swift Tutorial - Part 1

Hi developers, Today we're gonna learn what is data type, variable, constants, optionals and more in Swift Programming language. Let's get started!




Variables:


      Variables are used to store some values, when you creating a new variable the system will allocate some memories in memory(RAM). In that memory, you can store any values with any data type.

Data Types:


      Data Types are normally used to store values in variables with some type such as Int, Float, Double, Boolean, Character etc.

             Integer - This data type is used for storing a whole numbers which means you can store only the numbers without decimal points. Integers have two types they are signed and unsigned integer. Signed Integers are represented by Int8, Int16, Int32 and Int64. Unsigned Integers are represented by UInt8, UInt16, UInt32 and UInt64.

 
    var myInt = 4

    //or you can use the Int as datatype to create variables
   
    var myIntTwo: Int = 5 

             Float - This data type is used to store floating point numbers. It's smaller in size when compared to Double and it holds 32 bit floating point number with 6 decimal places.

 
    var myFloat = 4.1459

    //or you can use the Float as datatype to create variables
   
    var myFloatTwo: Float = 4.1459 

             Double - This data type is also used to store floating point numbers. It's bigger in size when compared to Float and it holds 64 bit floating point number with 15 decimal places.

 
    var myDouble = 4.1459

    //or you can use the Double as datatype to create variables
   
    var myDoubleTwo: Double = 4.1459 

             Boolean - This data type is used to store true or false value. It is used for conditions.

 

let isFirstTime = false
      
let noInternetConnection = ture



             String - It is used to store collections of characters within the double quotes.

 
    
       var myString = "Hello iOS Developers"

       //or you can use the String as datatype to create variables

       var myStringTwo: String = "Hello iOS Developers"
 


             Character - It is used to store a single character such as alphabetic letter, symbols etc. Swift stores the character internally as a grapheme cluster(made up of two or more unicode point).


  
    
       var myCharacter = "R"

       //or you can use the Character as datatype to create variables

       var myCharacterTwo: Character = "V"
 


Type Aliases:

 

         In swift, you can create your own name for default data types such as Int, Float, Double etc.

 
    
     typealias APILevel = Int
  
     var android: APILevel = 4

     print(android)
 


Type Inference:

 

         Swift compiler automatically detects which compiler you're using for your variable. So you no need to specify type for the variables.

 
    
     var varOne = 4  //It is inferred to type Int
     print(varOne)

     var varTwo = 2.3453    //It is inferred to type Float
     print(varTwo)
 

Type Annotation:


         You can provide the data type while creating the variables to represent what kind of values the variable can store.

 
    
     var varOne: Int = 4  //It is type annotation
     print(varOne)

     var varTwo: Float = 2.3453    //It is not a type annotation
     print(varTwo)
 

Semicolons:


          In swift, you no need to use semicolons for each statement but if you're writing the statement next to one another then you've to use semicolons to separate the statements.

 
    
     var varOne: Int = 4; print(varOne)


Printing Variable:


          In swift, you can print the strings using print() method and also you can interpolate a variable value by adding the variable inside the open and close parenthesis and escape it with a backslash.


  
    
       var iosVersion = 9

       print("You're using iOS Version \(androidVersion) in your iPhone") 
 


Optional:

 

       Optionals in swift can hold either a value or no value. According to Apple's documentation. Swift also introduces optional types, which handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”. Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes. 


 
    
  import Cocoa

  var myStringVar: String? = nil

  if myStringVar != nil { 

    println(myStringVar) 

    }else {
 
     println("It has nil value") 

  }

 



Output: 

    myString has nil value

      Automatic Unwrapping:


               By using exlamation mark instead of question mark, you no need to unwrap the optionals manually. It is automatically unwrap the optionals for you and you can get the assigned values correctly.


 
    
 
   import Cocoa

   var myStringVar: String!

   myStringVar = "Hello, Swift developers"

  if myStringVar != nil { 

     println(myStringVar)

  }else { 

    println("It has nil value")  

 } 
Output: 
 Hello, Swift developers

      Forced Unwrapping:


               If you declared a variable as Optionals using question mark. To get the value of that variable(optionals) you've to unwrap it. To unwrap, you need to put exlamation mark at the end of the variable

 
    
 
  import Cocoa

  var myStringVar: String?

  myStringVar = "Hello, Swift developers"

  if myStringVar != nil {

     println( myStringVar! )  //Unwrapping the variable

}else {

     println("It has nil value")

} 
Output: 
     
 Hello, Swift developers
If you didn't unwrap it then your output will look like below

  Optional("Hello, Swift developers")
 

Constants:

 

       Constants are used for creating a variable with fixed values. The values of the constant variable doesn't change while execution. Once you defined your values then you can't change it anywhere. Constants can be used with data types like String, Int, Float and Double etc.

To make the variable as constants you've to use let instead of var


 
    
    import cocoa

    let myconstant = 5
    print(myconstant)


Name your constants with letters, digits and underscore symbol. It must begins with letter or underscore character.

Download Playground files :