Swift Tutorial - Tutorialspoint

(backadmin) #1
println("Student name is specified")
}

let blankname = studrecord(stname: "")
if blankname == nil {
println("Student name is left blank")

}

When we run the above program using playground, we get the following result:


Student name is specified
Student name is left blank

Failable Initializers for Enumerations


Swift language provides the flexibility to have Failable initializers for enumerations too to
notify the user when the enumeration members are left from initializing values.


enum functions {
case a, b, c, d
init?(funct: String) {
switch funct {
case "one":
self = .a
case "two":
self = .b
case "three":
self = .c
case "four":
self = .d
default:
return nil
}
}
}

let result = functions(funct: "two")
if result != nil {
Free download pdf