A.swift
class A: UIView {
override init() {
super.init()
println("init A")
}
override init(frame: CGRect) {
super.init(frame: frame)
println("initFrame A")
}
}
B.swift
class B: A {
override init() {
super.init()
//P1
println("init B")
}
override init(frame: CGRect) {
super.init(frame: frame)
println("initFrame B")
}
}
Then I call it B()
:
I have an output:
initFrame A
initFrame B
init A
init B
I try to determine what? is called and when?... after B()
. I wish to understand it completely.
init()
in A
super.init()
in init()
in A
init()
in B
super.init()
in init()
in B
init()
in UIView
super.init()
in init()
in UIView
now we are in P1
point, right?
init()
calls init(frame:)
in B
with CGRectZero
super.init(frame:)
in init(frame:)
in B
init(frame:)
in A
super.init(frame:)
in init(frame:)
in A
init(frame:)
in UIView
super.init(frame:)
in init(frame:)
in UIView
now we are getting back
init(frame:)
in UIView
init(frame:)
in A
--> initFrame Ainit(frame:)
in B
--> initFrame BThe question is what is happening now? Where we are now? (inside init()
in UIView
?) Where are printed the lines with init A
and init B
?
Thanks for help.
B()
calls B
's init()
init()
defined in B
's superclass A
(due to super.init()
in B
's init()
)init()
defined in A
's superclass UIView
(due to super.init()
in A
's init()
)UIView
's init()
invokes init(frame: CGRectZero)
on the current instanceB
, and class B
overrides init(frame: CGRect)
, B
's own implementation of the method is invokedA
's init(frame: CGRectZero)
(due to super.init(frame: frame)
in B
's init(frame: CGRect)
)UIView
's init(frame: CGRectZero)
is called (due to super.init(frame: frame)
in A
's own implementation)A
's init(frame: CGRectZero)
(point 6 in the list), which prints initFrame A
B
's init(frame: CGRectZero)
(point 5), which prints initFrame B
UIView
's init()
(point 3), which doesn't print anythingA
's init()
(point 2), that prints initA
B
's init()
(point 1), which prints initB
, that you marked with P1
Please let me know if the steps are clear, of if I need to add more details to improve the explanation: I know it's a bit convoluted.