Swift Tutorial - Tutorialspoint

(backadmin) #1
println("With In Block Two")
}

let badresult = functions(funct: "five")
if badresult == nil {
println("Block Does Not Exist")

}

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


With In Block Two
Block Does Not Exist

Failable Initializers for Classes


A failable initializer when declared with enumerations and structures alerts an initialization
failure at any circumstance within its implementation. However, failable initializer in
classes will alert the failure only after the stored properties have been set to an initial
value.


class studrecord {
let studname: String!
init?(studname: String) {
self.studname = studname
if studname.isEmpty { return nil }
}
}
if let stname = studrecord(studname: "Failable Initializers") {
println("Module is \(stname.studname)")
}

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


Module is Failable Initializers

Overriding a Failable Initializer


Like that of initialize the user also has the provision to override a superclass failable
initializer inside the sub class. Super class failable initialize can also be overridden with in
a sub class non-failable initializer.

Free download pdf