I have subclasses of UINavigationController
and UITableViewController
.
To initialize subclasses I decided to use some convenience init
methods that call some designated initializer of the superclass. Also, each subclass has some let
constant:
let someValue: SomeClass = SomeClass()
Each class is successfully initialized by calling its newly created convenience init
method.
The problem is that the let
constant is initialized TWICE in UINavigationController
subclass.
import UIKit
import PlaygroundSupport
final class Navigation: UINavigationController {
convenience init(anyObject: Any) {
self.init(rootViewController: UIViewController())
}
let service = Constant("Constant Initialization -> Navigation")
}
final class Table: UITableViewController {
convenience init(anyObject: Any) {
self.init(style: .plain)
}
let service = Constant("Constant Initialization -> Table")
}
class Constant: NSObject {
init(_ string: String) {
super.init()
debugPrint(string)
}
}
Navigation(anyObject: NSNull())
Table(anyObject: NSNull())
Are we allowed to use convenience init
like above? Why?
Why is the convenience init
behavior is different in these two cases?
Checked with: Version 8.2 beta (8C30a), Version 8.2 (8C38), Version 8.2.1 (8C1002)
P.S. Playground log of the code above:
"Constant Initialization -> Navigation"
"Constant Initialization -> Navigation"
"Constant Initialization -> Table"