Swift 3 Variable

Swift 3 variables

var str = "Hello, playground"
print(str)

Other samples

var current : Int = 0
var pai : Double = 3.141592
var x=0.0, y=1.0

Convert Double to Int

var d = 100.0
print(Int(d))

We can use Double() as well to convert to Double

var myInt : Int = 23
print(Double(myInt))

Max Variable

Int.max
Int64.max
Int32.max
UInt32.max

e

10e2         // 1000

Optional

Optional is allowed to populate nil

Sample

var strNil : String? = nil
var strOp : Optional<String> = nil

Force unwrap

var unwrapInt : Int? = 5
var countInt : Int = 6
var res = unwrapInt! + countInt

// Implicitly Unwrapped Optional
var unwrappedInt : Int! = 5

Optional Chaining

class Company {
    var name : String?
}

var comp = Company()
comp.name = "ABC"
print(comp.name?.characters.count)