Planet name is: Mercury
No Planets like that: [No Planets]
The init! Failable Initializer
Swift provides 'init?' to define an optional instance failable initializer. To define an implicitly
unwrapped optional instance of the specific type 'init!' is specified.
struct studrecord {
let stname: String
init!(stname: String) {
if stname.isEmpty {return nil }
self.stname = stname
}
}
let stmark = studrecord(stname: "Swing")
if let name = stmark {
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
Required Initializers
To declare each and every subclass of the initialize 'required' keyword needs to be defined
before the init() function.
class classA {
required init() {