OPTIONALS MASTERCLASS

Optionals say either "there is a value, and it equals x" or "there isn't a value at all". An Optional is a type on its own, actually one of Swift 4's new super-powered enums. It has two possible values, None and Some(T), where T is an associated value of the correct data type available in Swift 4.

 

//OPTIONALS MASTERCLASS

//If you want to create a variable with "no value" just add a "?", or "!", depending on the case.

//EXAMPLE

var number: Int?

print (number as Any)

//ANOTHER EXAMPLE THAT WILL NOT CRASH

let enteredText = "four"

let enteredTextInteger = Int (enteredText)

if let anotherText = enteredTextInteger{

    print(anotherText * 2)

}else{

    print("Please try again. Invalid input.")

}


Happy coding! :)