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
Childtake both atitleand asubtitle, and then callsuper.init(title: title)in yourChild's initializer:Then if you create a
Childobject and assign it to an OptionalChild(i.e.Child?) you'll see both initialized messages:and then if you assign
nilto your variable you'll see both deinitialized messages: