Create a class called Parent
with a title
property and write an init
and deinit
method for your class.
Write a subclass called Child
.
My problem is putting this into the code (call super.init(title:)
and pass on the title
parameter.
class Parent {
let title: String
init(title: String){
self.title = title
print("\(title) is initialized")
}
deinit {
print("\(title) is being deinitialized.")
}
}
class Child: Parent {
let subtitle: String
init(subtitle: String){
self.subtitle = subtitle
// i'm supposed to call a super.init, how
print("\(subtitle) is initialized")
}
deinit {
print("\(subtitle) is being deinitialized.")
}
}
Make your initializer for your
Child
take both atitle
and asubtitle
, and then callsuper.init(title: title)
in yourChild
's initializer:Then if you create a
Child
object and assign it to an OptionalChild
(i.e.Child?
) you'll see both initialized messages:and then if you assign
nil
to your variable you'll see both deinitialized messages: