3
3
.
.
3
3
V
V
a
a
r
r
i
i
a
a
b
b
l
l
e
e
s
s
I
I
n
n
f
f
o
o
Constants & Variables hold single Value.
Optionals are Constants or Variables that can have nil value (question mark '?' indicates that variable might be empty).
Enumerators either define subset of discrete read only Values or Templates (combination of different Data Types).
C
C
o
o
n
n
s
s
t
t
a
a
n
n
t
t
s
s
&
&
V
V
a
a
r
r
i
i
a
a
b
b
l
l
e
e
s
s
Constants & Variables
let name : String = "John" //CONSTANT
var age : Int = 20 //VARIABLE
O
O
p
p
t
t
i
i
o
o
n
n
a
a
l
l
s
s
Optional Scalars
let name : String? = "John" //OPTIONAL CONSTANT ('?' indicates that constant might not have value - nil)
var age : Int? = 20 //OPTIONAL VARIABLE ('?' indicates that variable might not have value - nil)
Unwrap Optional
//FORCED UNWRAP.
print(name!)
//UNWRAP OPTIONALS.
if let unwrapedName = name, var age = age {
print("\(unwrapedName) is \(age) years old")
}
Optional Chaining
if let room = building?.getFloor(8)?.getFlat(10)?.getRoom("bathroom")? {
print(room )
}
E
E
n
n
u
u
m
m
e
e
r
r
a
a
t
t
o
o
r
r
s
s
Value Enumerator is a subset of discrete read only values of certain Data Type: String, Int, Float. (that have Aliases)
Template Enumerators allow you to select a Template - combination of different Data Types: String, Int, Float.
Then you give value to each Data Type in predefined order (even if Parameters are named).
Initializer is used to assign default Alias to Variable.
Value Enumerator
//DECLARE VALUE ENUMERATOR.
enum Errors : Int { //Eumerator will hold values of Int Data Type
//DECLARE ALIASES. //Values and Aliases must be unique
case Error0 = 0 //Declare alias and its value.
case Error1 = 10 //Declare alias and its value.
case Error2 = 20 //Values must be of declared Data Type Int.
case Error3 //Value is automaticaly set to 31 (next integer).
case Error4 = 40 //Value is set to 40.
//DECLARE INITIALIZERS. //They specify Alias that will be assigned to a Variable.
init() { self = .Error0 }
init(x: Int) { self = .Error1 }
init(x: Int, y: Int) { self = .Error2 }
}
//DECLARE VALUE ENUMERATOR VARIABLE.
var error : Errors = Errors.Error1 //Variable can store subset of Int values defined by enum
var error0 : Errors = Errors() //Errors.Error0
var error1 : Errors = Errors(x: 1) //Errors.Error1
var error2 : Errors = Errors(x: 2, y: 2) //Errors.Error2
//DISPLAY VARIABLE.
print(error) //Error1 (Alias)
print(error.rawValue) //10 (Value)
Template Enumerator
//DECLARE TEMPLATE ENUMERATOR.
enum Errors {
//DECLARE TEMPLATE ALIASES. //Aliases must be unique
case Exception //No Parameters
case State (Int) //Single Parameter
case Error (Int, String) //Error(1,"RED"), Error(2,"BLUE")
case Warning (code:Int, message:String) //Named Parameters.
//DECLARE INITIALIZERS. //They specify Template & values
init() { self = .Exception }
init(x: Int) { self = .State(x) }
}
//DECLARE TEMPLATE ENUMERATOR VARIABLES.
var error1 : Errors = Errors.Exception //Exception No Parameters
var error2 : Errors = Errors.State (1) //State (1)
var error3 : Errors = Errors.Error (1, "RED") //Error (1, "RED")
var error4 : Errors = Errors.Warning(code:1, message:"RED") //Warning(1, "RED") Can't change order
var error5 : Errors = Errors() //Exception
var error6 : Errors = Errors(x: 1) //State(1)