Swift basic concepts - Variables
 
  March 5, 2019

 Variables

            

// Variable declaration
var expressiveAssigment : String = "Value"
print(expressiveAssigment)

var expressive : String = ""
expressive = "Value"
print(expressive)

var intuitive = "Guest What?"
print(intuitive)

// Contants
let constantAssigment : String = "Constant Value"
print(constantAssigment)

let pi = 3.1416
print(pi)

// Optionals
var optionalString : String? = nil
var optional : Int? = nil

// How to work with optionals
print(optional) // nil
print(optional ?? 0) // Provide a default value
print(optional as Any) // Explicit Cast to Any to avoid errors
print(optional!) // Force-unwrap. CAUTION: Throw error if value is nil

// Optional Binding
if let opcbin = opcional {
print(opcbin)
} else {
print("No value")
}

// Strings
var phrase = "World!"
print("Hello " + phrase)
print("Hello ",phrase)
print("Hello \(phrase)")

let stringMultiline = """
To create a multine text
we must use three quotes
the end quotes must be on a new line
"""

let simpleCharacter : Character = "A"

// Cmd + Ctrl + Space
var emojis = "😇"

// Concatenation
emojis.append(simpleCharacter)

// String Functions
emojis.count
emojis.isEmpty
stringMultilinea.prefix(10)

// Swift 5 - Raw Strings
var rawString = #"Use "quotes" without escape sequences"#
var rawString2 = #"Use "quotes" along with a variable \#(phrase)"#